This is active.rb
class Active < ActiveResource::Base
self.site = "http://localhost:3002/api/v1/users" # **When i run this it is not fetching data**
self.site = Net::HTTP.get(URI.parse("http://localhost:3002/api/v1/users")) # **When i run this i can see the data in console. Will get error Bad URI**
end
welcome_controller.rb
def index
@active = Active.all
end
I am Unable to fetch data from the using active resource. Please let me know Thank you
I suspect that ActiveResource is not making the request you are expecting. You can get some clarity by running the following in the Rails console:
Active.collection_pathandActive.element_pathfor the former you will see
"/api/v1/users/actives.json"as activeresource expects the class Active to be the name of your resource.You can control the URI generated and remove the resource specification (ie .json) by overriding two of the ActiveResource methods
This would then give you a collection path of
/api/v1/usersPerhaps a cleaner option would be to use
self.element_name = "users", the documentation indicates that this " In the case where you already have an existing model with the same name as the desired RESTful resource"You can also remove the format (.json) by using
self.include_format_in_path = falseas mentioned here.So you could have the same affect by using:
Just as an aside I'd like to link to this answer which has some extremely useful notes on customising ActiveResource without recourse to monkey patching.