开发者

Building a polymorphic model controller path with has_many

开发者 https://www.devze.com 2022-12-17 19:48 出处:网络
I have a polymorphic model Comment that can be related to many types of commentables. in my routs, for example I have:

I have a polymorphic model Comment that can be related to many types of commentables. in my routs, for example I have:

map.resources :newsitems do |news|
  news.resources :comments
end

everything is working fine, the on开发者_运维百科ly problem is to generate the paths. I've in my views/controller the @commentable item, that i retrieve from a before_filter.

Links to [@commentable, @comment] works fine, like forms, show, or destroy. but links to new and edit are messed... comments_path(@commentable, @comment) doesn't work for example.

how could i build this dynamic path in my views?

especially the edit_ and new_ paths


I use polymorphic_path for this, which requires :action for :new and :edit, like so:

link_to("New Comment", polymorphic_path([@commentable,@comment], :action => :new))

http://api.rubyonrails.org/classes/ActionController/PolymorphicRoutes.html#M000487

0

精彩评论

暂无评论...
验证码 换一张
取 消