Associations:
location has_many :comments
comment belongs_to :location
For some reason, this GET:
/locations/5/comments.json
is acting like this GET:
/comments.json
Started GET "/locations/5/comments.json" for 127.0.0.1 at 2012-04-10 21:18:00 -0700
Processing by CommentsController#index as JSON
Parameters: {"location_id"=>"5"}
Comment Load (0.1ms) SELECT "comments".* FROM "comments"
Completed 200 OK in 21ms (Views: 1.0ms | ActiveRecord: 0.7ms)
Note the SQL query: SELECT “comments”.* FROM “comments”
The route is set up like this:
resources :locations do
resources :comments
end
Rake routes confirms the route:
location_comments GET /locations/:location_id/comments(.:format) {:action=>"index", :controller=>"comments"}
Here is the index action:
def index
@comments = Comment.all
respond_to do |format|
format.json { render json: @comments }
end
end
Is this the right action? It is consistent with the result, but I am not sure what else should be here. I’ve never had a problem with nested resources before, so I’ve never looked into the details.
try this: