问题
I have this routes:
resources :tags do
resources :comments
end
so the :create
action for the comments has the following form
tag_comments POST /tags/:tag_id/comments(.:format)
how can i change the paramenter name from :tag_id
to :commentable_id
?
回答1:
map.tags do
resources :comments, :path_prefix => '/tags/:commentable_id'
end
or via before_filter
before_filter :tag2commentable
private
def tag2commentable
params[:commentable_id] = params[:tag_id] unless params[:tag_id].blank?
end
put it in your controller
回答2:
One of these may be what you want:
map.resources :commentables, :as => "tags", :collection => :comments
map.resources :commentables, :as => "tags", :has_many => :comments
I suppose the latter being correct, which resolves to:
$ rake routes
commentable_comments GET /tags/:commentable_id/comments(.:format) {:action=>"index", :controller=>"comments"}
...
But I suppose your model relations may be screwed somehow as this makes no sense. Do mind to amend your post and add info about your model relations?
I suppose having
map.resources :commentables, :has_many => :tags
or
map.resources :taggables, :has_many => :comments
would make more sense:
commentable_tags GET /commentables/:commentable_id/tags(.:format) {:action=>"index", :controller=>"tags"}
taggable_comments GET /taggables/:taggable_id/comments(.:format) {:action=>"index", :controller=>"comments"}
来源:https://stackoverflow.com/questions/2983828/change-param-name-to-nested-resource-parent