Tia*_*ago 2 ruby-on-rails path polymorphic-associations
我有一个多态模型Comment,可以与许多类型的commentable相关.在我的溃败中,例如我有:
map.resources :newsitems do |news|
news.resources :comments
end
Run Code Online (Sandbox Code Playgroud)
一切都很好,唯一的问题是生成路径.我在我的视图/控制器中有@commentable项,我从before_filter中检索.
链接到[@commentable,@ comment]工作正常,如表单,显示或销毁.但是新的和编辑的链接是混乱的...例如,comments_path(@commentable,@ comment)不起作用.
我怎么能在我的视图中构建这个动态路径?
特别是edit_和new_路径
我使用polymorphic_path,这需要:action for:new和:edit,如下所示:
link_to("New Comment", polymorphic_path([@commentable,@comment], :action => :new))
http://api.rubyonrails.org/classes/ActionController/PolymorphicRoutes.html#M000487