Change param name to nested resource parent

≯℡__Kan透↙ 提交于 2019-12-12 01:35:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!