multiple nested ressources

Hi all

I've got a little problem with my restfull application. I have 2 different resourcse i want to map with 1 nested resources.

In my application both game and article resources can be commented, comment is a polymorphic relation for game and article. Annd all comment stuf is located in the CommentsController.

My routes.rb file look like as below : [...]   map.resources :articles do |articles|     articles.resources :comments   end

  map.resources :game do |games|     games.resources :comments   end [...]

When i use the comment_path(@article) method it generate a url like this /games/1/comment.

How i can generate an /article/1/comment , and a /games/1/comment urls in this configuration. Or what I lmust change in my routes.rb file to achive this goals.

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: <; 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.


In my view I use the commentable relation. For exemple in my new comment view (it is shared by commentable resources) I pass the commentable obect in the locals variable when i call the render method

<% form_for(:comment, :url => send("#{}_comments_path", commentable)) do |f| %>   <fieldset>     <p><%= label_tag 'comment_body', 'Votre commentaire' %><br>     <%= f.text_area :body %></p>     <p class="buttons">       <%= submit_tag "Sousmettre" %> ou <%= link_to 'Annuler' %>     </p>   </fieldset>

<% end %>

or in my show comment view, i use the same tric

<% div_for comment do %>   <div class="right">     <%= link_to "Supprimer", send("#{comment.commentable_type.downcase}_comment_path", comment.commentable, comment), :method => :delete %>   </div>   <%= comment.body %> <% end %>

Hope this help. I you have better or cleaner way to go tell me.