协会:
location has_many :comments
comment belongs_to :location
出于某种原因,这个 GET:
/locations/5/comments.json
像这样 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)
注意 SQL 查询:SELECT "comments".* FROM "comments"
路线是这样设置的:
resources :locations do
resources :comments
end
耙路线确认路线:
location_comments GET /locations/:location_id/comments(.:format) {:action=>"index", :controller=>"comments"}
这是索引操作:
def index
@comments = Comment.all
respond_to do |format|
format.json { render json: @comments }
end
end
这是正确的行动吗?它与结果一致,但我不确定这里应该还有什么。我以前从来没有遇到过嵌套资源的问题,所以我从来没有研究过细节。