In working on a RESTful website, I’ve encountered a stumbling block
involving polymorphic associations.
For example, given:
map.resources :posts do |post|
post.resources :comments, :name_prefix => ‘post_’
map.resources :photos do |photo|
photo.resources :comments, :name_prefix => ‘photo_’
Where comments is polymorphic. The acts_as_commentable plugin fits the
So we have routes like:
If we want to create a new comment for photo 678 we would send a POST to
/posts/678/comments. Nice and RESTful.
But… how do I generate the path for the form target if the comment
form is in a shared partial (DRY)?
I did find one post about this on Rails Weenie and the answer was more
of a workaround… an ugly helper that uses link_to. Is there any way to
do this gracefully, such as *_path?
<% form_for(comment_path(@photo)) %>
…would be smart enough to determine the object type of @photo and
generate the correct /firstname.lastname@example.org/comments route automatically.
Any thoughts? Anyone run into this, and if so how did you solve it?