views:

189

answers:

1

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 only 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

+4  A: 

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

Eric Hill