Hi Guillaume,
I'm currently attacking the same problem, in the context of users and
locations sharing a polymorphic address model.
For your routes, you'll need to find some way to separate the two
different types of comments, in my code, I've used name_prefixes:
[...]
map.resources :articles do |articles|
articles.resources :comments, :name_prefix => 'article_'
end
map.resources :games do |games|
games.resources :comments, :name_prefix => 'game_'
end
[...]
This allow me to use: article_comment_path(@article, @comment) which
gives me a url like: /articles/1/comments/2
Or game_comment_path(@game, @comment) which gives me:
/games/1/comments/2.
There's some really cool suggestions in the Rails Weenie forum:
<http://rails.techno-weenie.net/forums/1/topics/642> on how to modify
your controller to determine the polymorphic Type and ID for comment
before performing an index or create operation.
The place where I'm stuck is in the Views, and I'd welcome any help on
the subject. Currently, I have one views directory (in my case it's
addresses) with new, edit, show, index. In each of those files, I have
conditional statements that look like this:
[...]
<% unless params[:user_id].nil? %>
<%= link_to 'Edit', user_edit_address_path(params[:user_id], @address)
%> |
<%= link_to 'Back', user_addresses_path(params[:user_id]) %>
<% else %>
<%= link_to 'Edit', location_edit_address_path(params[:location_id],
params[:organization_id], @address) %> |
<%= link_to 'Back', location_addresses_path(params[:location_id],
params[:organization_id]) %>
<% end %>
Not very DRY. But, with code like that in my views, ebryn's
'find_by_polymorphic_type_and_id' code, and the routes.rb code I've
outlined above, it does all work.
-Jared