Search code examples
ruby-on-rails-3apirestactiveresource

Wrong URL when consuming existing Java REST API using rails ActiveResource


I am new to ruby on rails.I am trying to consume my existing Java REST api using Rails, ActiveResource class, but every time resource URL hit, is wrong (https://myapp/resource/**apis**/responce.json), where the correct url is (https://myapp/resource/**api**/responce.json)

Problem is active resource mapping apis/ instead of api/

Below is my code for model => app/model/api.rb

class Api < ActiveResource::Base
  self.site = "https://myapp/resource"
  self.format = :json
  self.element_name = "api"
end

Code in app/controllers/api_controller.rb

@api = Api.get(:responce, :key => "key", :userId =>'1')

The above code always give 404 error, when I check the logs, resource is hitting https://myapp/resource/**apis**/responce.json?key=key&userId=1 Where it must hit resource URL as https://myapp/resource/**api**/responce.json


Solution

  • The self.element_name = "api" is fixing the pluralization when you are doing a find(:first) or similar. What you need is self.collection_name = "api". That should fix the URL it is creating.