# Linking to RESTful urls within a for loop

**URL:** <https://www.ruby-forum.com/t/linking-to-restful-urls-within-a-for-loop/88321>\
**Category:** Rails\
**Created:** [March 9, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/linking-to-restful-urls-within-a-for-loop/88321 "2007-03-09T18:01:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [March 9, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/linking-to-restful-urls-within-a-for-loop/88321/1 "2007-03-09T18:01:01Z")

</div>

I’m trying to use the new RESTful style of development, but am running  
into a problem.

I’m trying to show a list of comments, each of which belongs to a user.  
And have a link to the user’s page at the bottom of each comment.

In the routes, there is “map.resources :users”. The problem is when  
using a link\_to within a for loop. I don’t know what to pass to  
users\_url.

1. I’ve tried users\_url(comment.user.id), users\_url(comment.user), and a  
few other things like that, but that gives the error of “You have a nil  
object when you didn’t expect it! The error occurred while evaluating  
nil.to\_sym”.

2. So next, i tried users\_url(:id =\> alert.user.id). Which didn’t  
through an error. But the links don’t work with “map.resources :users”  
because they look like this: /users?id=2

What is the proper syntax in this case?

I’m using  
\<% for comment in @comments %\>

#### \<%= h(comment.title) %\>

\<%= h(comment.message) %\>

Submitted by: \<%= link\_to( h(comment.user.name), users\_url(comment.user.id) ) %\>

\<% end %\>

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [March 10, 2007, 3:09am UTC](https://www.ruby-forum.com/t/linking-to-restful-urls-within-a-for-loop/88321/2 "2007-03-10T03:09:43Z")

</div>

On Mar 9, 2007, at 11:01 AM, Tim Michaud wrote:

> In the routes, there is “map.resources :users”. The problem is when  
> using a link\_to within a for loop. I don’t know what to pass to  
> users\_url.

You want user\_url(user) not users\_url() … the former is for 1  
user, the later is for all of them.
