4

Is there a configuration option avilable in ActiveResource to change the request url structure

for example when my client application try to access services of particular user from the api, ActiveResource sending request to api url in the following structure

http://localhost:3000/api/v1/services.json?user_id=1

but instead i want the ActiveResource to send request to api url like this

http://localhost:3000/api/v1/users/1/services

These are the two model files i am using in my client rails applicaiton

user.rb

 class User < ActiveResource::Base 
   self.site = "http://localhost:3001/api/v1"
   has_many :services
 end

service.rb

 class Service < ActiveResource::Base
   self.site = "http://localhost:3001/api/v1"
   belongs_to :user 
 end

Any help , would be greatly appreciated.Thanks

  • you use the same url for the two models, shoudln't the second be different ? the correct url is defined by the receiving server, namely which route you define – peter Aug 15 '15 at 13:05
  • @peter thanks for your reply, i don't know what you men by 'souldin't the second be different',why because `self.site` is the attribute where we use to assign the base api url, this need not to be different , i am using a single api , so the second one need not to be different. – Ramesh Kumar Thiyagarajan Aug 15 '15 at 13:11
  • well, how does your server know what request you make, for a User or a Service ? I think you would need at least 2 routes to serve both of them, but hey, I have no experience with ActiveResouce so I could be wrong – peter Aug 15 '15 at 13:16
  • @peter yes the correct URL is defined by the receiving server and the receiving server URL format is this `http://localhost:3000/api/v1/users/1/services` , when i trying to access relationship child record , the active source is sending request to api in this structure `http://localhost:3000/api/v1/services.json?user_id=1`, so that is why i want to change the active source request URL structure. – Ramesh Kumar Thiyagarajan Aug 15 '15 at 13:18
  • Anyway thanks for your try @peter – Ramesh Kumar Thiyagarajan Aug 15 '15 at 13:21
  • i see, have you seen this post about overriding the site method ? http://railscasts.com/episodes/95-more-on-activeresource?view=comments – peter Aug 15 '15 at 15:00

1 Answers1

0

With these models:

 class User < ActiveResource::Base 
   self.site = "http://localhost:3001/api/v1"
   has_many :services
 end

 class Service < ActiveResource::Base
   self.site = "http://localhost:3001/api/v1"
   belongs_to :user 
 end

ActiveResource should make the requests as follows:

user = User.find(1)      # GET http://localhost:3001/api/v1/users/1.json
services = user.services # GET http://localhost:3001/api/v1/users/1/services.json

Assuming you are going to have more options in your ActiveResource, you may consider using something like this as well:

 class Resource < ActiveResource::Base 
   self.site = "http://localhost:3001/api/v1"
 end

 class User < Resource 
   has_many :services
 end

 class Service < Resource
   belongs_to :user 
 end
Tom Rossi
  • 11,604
  • 5
  • 65
  • 96