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

**URL:** <https://www.ruby-forum.com/t/link-to-comments-article-comments/138242>\
**Category:** Rails\
**Created:** [May 21, 2008, 9:52am UTC](https://www.ruby-forum.com/t/link-to-comments-article-comments/138242 "2008-05-21T09:52:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Izidor\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ee59a6/32.png) [@Izidor\_J](https://www.ruby-forum.com/u/Izidor_J)\
**Post date:** [May 21, 2008, 9:52am UTC](https://www.ruby-forum.com/t/link-to-comments-article-comments/138242/1 "2008-05-21T09:52:20Z")

</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\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ee59a6/32.png) [@Izidor\_J](https://www.ruby-forum.com/u/Izidor_J)\
**Post date:** [May 21, 2008, 9:57am UTC](https://www.ruby-forum.com/t/link-to-comments-article-comments/138242/2 "2008-05-21T09:57:38Z")

</div>

On 21.5.2008, at 9:52, Izidor J. wrote:

> @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\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ee59a6/32.png) [@Izidor\_J](https://www.ruby-forum.com/u/Izidor_J)\
**Post date:** [May 21, 2008, 10:05am UTC](https://www.ruby-forum.com/t/link-to-comments-article-comments/138242/3 "2008-05-21T10:05:19Z")

</div>

On 21.5.2008, at 9:56, Izidor J. wrote:

> > good for me…
> > 
> > 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:** ![Izidor\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ee59a6/32.png) [@Izidor\_J](https://www.ruby-forum.com/u/Izidor_J)\
**Post date:** [May 21, 2008, 10:11am UTC](https://www.ruby-forum.com/t/link-to-comments-article-comments/138242/4 "2008-05-21T10: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\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ee59a6/32.png) [@Izidor\_J](https://www.ruby-forum.com/u/Izidor_J)\
**Post date:** [May 21, 2008, 10:35am UTC](https://www.ruby-forum.com/t/link-to-comments-article-comments/138242/5 "2008-05-21T10:35:21Z")

</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
