How to change request url structure in ruby โ€‹โ€‹gem activeresource

Is there an avilable configuration parameter in ActiveResource to change the structure of the request URL

for example, when my client application tries to access the services of a specific user from an api send request, ActiveResource for api url in the following structure

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

but instead I want ActiveResource to send a request to api url like this

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

These are two model files that I use in my client rail application.

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

+4
source share
1 answer

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 queries 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 have more features in ActiveResource, you can also use something like this:

  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 
0
source

All Articles