# multiple nested ressources

**URL:** <https://discuss.rubyonrails.org/t/multiple-nested-ressources/4748>\
**Category:** rubyonrails-talk\
**Created:** [November 19, 2006, 6:02pm UTC](https://discuss.rubyonrails.org/t/multiple-nested-ressources/4748 "2006-11-19T18:02:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dj\_T](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@Dj\_T](https://discuss.rubyonrails.org/u/Dj_T)\
**Post date:** [November 19, 2006, 6:02pm UTC](https://discuss.rubyonrails.org/t/multiple-nested-ressources/4748/1 "2006-11-19T18:02:57Z")

</div>

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 : [...] &nbsp;&nbsp;map.resources :articles do |articles| &nbsp;&nbsp;&nbsp;&nbsp;articles.resources :comments &nbsp;&nbsp;end

&nbsp;&nbsp;map.resources :game do |games| &nbsp;&nbsp;&nbsp;&nbsp;games.resources :comments &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Jared](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jared](https://discuss.rubyonrails.org/u/Jared)\
**Post date:** [November 21, 2006, 3:30pm UTC](https://discuss.rubyonrails.org/t/multiple-nested-ressources/4748/2 "2006-11-21T15:30:13Z")

</div>

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: [...] &nbsp;&nbsp;map.resources :articles do |articles| &nbsp;&nbsp;&nbsp;&nbsp;articles.resources :comments, :name\_prefix =\> 'article\_' &nbsp;&nbsp;end

&nbsp;&nbsp;map.resources :games do |games| &nbsp;&nbsp;&nbsp;&nbsp;games.resources :comments, :name\_prefix =\> 'game\_' &nbsp;&nbsp;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&gt](http://rails.techno-weenie.net/forums/1/topics/642&gt); 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? %\> &nbsp;&nbsp;\<%= link\_to 'Edit', user\_edit\_address\_path(params[:user\_id], @address) %\> | &nbsp;&nbsp;\<%= link\_to 'Back', user\_addresses\_path(params[:user\_id]) %\> \<% else %\> &nbsp;&nbsp;\<%= link\_to 'Edit', location\_edit\_address\_path(params[:location\_id], params[:organization\_id], @address) %\> | &nbsp;&nbsp;\<%= 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

---

<div class="post-metadata">

**Author:** ![Dj\_T](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@Dj\_T](https://discuss.rubyonrails.org/u/Dj_T)\
**Post date:** [November 23, 2006, 10:00pm UTC](https://discuss.rubyonrails.org/t/multiple-nested-ressources/4748/3 "2006-11-23T22:00:19Z")

</div>

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("#{commentable.class.name.downcase}\_comments\_path", commentable)) do |f| %\> &nbsp;&nbsp;\<fieldset\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\>\<%= label\_tag 'comment\_body', 'Votre commentaire' %\>\<br\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.text\_area :body %\>\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<p class="buttons"\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= submit\_tag "Sousmettre" %\> ou \<%= link\_to 'Annuler' %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;\</fieldset\>

\<% end %\>

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

\<% div\_for comment do %\> &nbsp;&nbsp;\<div class="right"\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= link\_to "Supprimer", send("#{comment.commentable\_type.downcase}\_comment\_path", comment.commentable, comment), :method =\> :delete %\> &nbsp;&nbsp;\</div\> &nbsp;&nbsp;\<%= comment.body %\> \<% end %\>

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