# link\_to 'comments', @article.comments ?

**URL:** <https://discuss.rubyonrails.org/t/link-to-comments-article-comments/29630>\
**Category:** rubyonrails-talk\
**Created:** [May 21, 2008, 7:52am UTC](https://discuss.rubyonrails.org/t/link-to-comments-article-comments/29630 "2008-05-21T07:52:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Izidor\_Jerebic](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@Izidor\_Jerebic](https://discuss.rubyonrails.org/u/Izidor_Jerebic)\
**Post date:** [May 21, 2008, 7:52am UTC](https://discuss.rubyonrails.org/t/link-to-comments-article-comments/29630/1 "2008-05-21T07:52:02Z")

</div>

Hello,

in a restful application, I am trying to create the thing mentioned int he subject: link to the association, e.g. /articles/1/comments.

The "link\_to 'comments', @article.comments" doesn't do anything good for me...

I can get the result via "link\_to 'comments', @article.comments.new", but creating new object just for link generation seems wrong (also, the intention is something completely different, the paths for 'create new comment' and 'comments' just coincide)...

I can also get it by "link\_to 'comments', url\_for\_article\_comments( @article )", but I would like to have a shorter version (especially when used in multi-level relationships)...

I am currently using Rails 2.0.1.

Anybody knows the official way to do it? Could an upgrade of Rails help?

izidor

---

<div class="post-metadata">

**Author:** ![Izidor\_Jerebic](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@Izidor\_Jerebic](https://discuss.rubyonrails.org/u/Izidor_Jerebic)\
**Post date:** [May 21, 2008, 7:56am UTC](https://discuss.rubyonrails.org/t/link-to-comments-article-comments/29630/2 "2008-05-21T07:56:55Z")

</div>

> Hello,
> 
> in a restful application, I am trying to create the thing mentioned int he subject: link to the association, e.g. /articles/1/comments.
> 
> The "link\_to 'comments', @article.comments" doesn't do anything good for me...
> 
> I can get the result via "link\_to 'comments', @article.comments.new", but creating new object just for link generation seems wrong (also, the intention is something completely different, the paths for 'create new comment' and 'comments' just coincide)...

Above is typo, I tried "link\_to 'comments', [@article, @article.comments.new]....

---

<div class="post-metadata">

**Author:** ![Izidor\_Jerebic](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@Izidor\_Jerebic](https://discuss.rubyonrails.org/u/Izidor_Jerebic)\
**Post date:** [May 21, 2008, 8:04am UTC](https://discuss.rubyonrails.org/t/link-to-comments-article-comments/29630/3 "2008-05-21T08:04:46Z")

</div>

> > Hello,
> > 
> > in a restful application, I am trying to create the thing mentioned int he subject: link to the association, e.g. /articles/1/comments.
> > 
> > The "link\_to 'comments', @article.comments" doesn't do anything good for me...
> > 
> > I can get the result via "link\_to 'comments', @article.comments.new", but creating new object just for link generation seems wrong (also, the intention is something completely different, the paths for 'create new comment' and 'comments' just coincide)...
> 
> Above is typo, I tried "link\_to 'comments', [@article, @article.comments.new]....
> 
> > I can also get it by "link\_to 'comments', url\_for\_article\_comments( @article )", but I would like to have a shorter version (especially when used in multi-level relationships)...

I am confused today: link\_to 'comments', article\_comments\_path( @article )" is of course my code.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 21, 2008, 8:11am UTC](https://discuss.rubyonrails.org/t/link-to-comments-article-comments/29630/4 "2008-05-21T08:11:47Z")

</div>

You're talking about nested resources here if I understand that right.

So what sense would it make to link the comments index action for a not existing article? Or are you using a namespace article?

---

<div class="post-metadata">

**Author:** ![Izidor\_Jerebic](https://avatars.discourse-cdn.com/v4/letter/i/47e85d/32.png) [@Izidor\_Jerebic](https://discuss.rubyonrails.org/u/Izidor_Jerebic)\
**Post date:** [May 21, 2008, 8:32am UTC](https://discuss.rubyonrails.org/t/link-to-comments-article-comments/29630/5 "2008-05-21T08:32:01Z")

</div>

Eh, I am a bit confused today... Let me try to explain...

The @article exists, and Rails could get to it from association  
object, I presume...

I would like to get /articles/1/comments in a shortes possible way. It  
could be programmed by taking the association object  
@article.comments, retrieving its parent object, and constructing  
path. But it doesn't happen currently.

The shortest way to get the path is "link\_to 'ccc', [@article,  
@article.comments.new]", but I don't like it...

The correct way is "link\_to 'ccc', article\_comments\_path( @article )",  
but it is long (especially for multi-level paths)...

izidor
