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_’
end
map.resources :photos do |photo|
photo.resources :comments, :name_prefix => ‘photo_’
end
Where comments is polymorphic. The acts_as_commentable plugin fits the
bill.
So we have routes like:
/posts/678/comments
/photos/185/comments
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?
Ideally:
<% form_for(comment_path(@photo)) %>
…would be smart enough to determine the object type of @photo and
generate the correct /photos/@photo.id/comments route automatically.
Any thoughts? Anyone run into this, and if so how did you solve it?
Thanks!