# Passing parameters through link\_to

**URL:** <https://www.ruby-forum.com/t/passing-parameters-through-link-to/122292>\
**Category:** Rails\
**Created:** [December 6, 2007, 9:07am UTC](https://www.ruby-forum.com/t/passing-parameters-through-link-to/122292 "2007-12-06T09:07:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TTDaVeTT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/91b2a8/32.png) [@TTDaVeTT](https://www.ruby-forum.com/u/TTDaVeTT)\
**Post date:** [December 6, 2007, 9:07am UTC](https://www.ruby-forum.com/t/passing-parameters-through-link-to/122292/1 "2007-12-06T09:07:44Z")

</div>

Is it possible to pass parameters through link\_to that will be used by  
the controller that link\_to directs to? Specifically I have this  
code:  
\<% for user in @users -%\>  
\<%= link\_to user.screen\_name, {:action =\> “index”, :controller =\>  
“users”, :user\_id =\> user.id } %\>  
\<% end %\>

Is it possible to use the parameter at the end of link\_to(:user\_id =\>  
user.id) to initialize a hash @user using the user\_id?

This is the index action in the users controller:  
def index  
@user = User.find(:all, :conditions =\> { :id =\> :user\_id })  
end

I dont know if this makes sense but ultimately I am trying to link to  
a profile and have that profile be filled with the user that matches  
the id of the link that is clicked. So I am trying to pass the id of  
the user through link\_to and use that id in the index action above to  
populate a hash with the corresponding user. Im not sure if this is  
the way to go about it. Ive been stuck on this for a while, so any  
input is much appreciated. Thanks, Dave

---

<div class="post-metadata">

**Author:** ![TTDaVeTT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/91b2a8/32.png) [@TTDaVeTT](https://www.ruby-forum.com/u/TTDaVeTT)\
**Post date:** [December 6, 2007, 10:59am UTC](https://www.ruby-forum.com/t/passing-parameters-through-link-to/122292/2 "2007-12-06T10:59:02Z")

</div>

On 6 Dec 2007, at 08:07, TTDaVeTT wrote:

> user.id) to initialize a hash @user using the user\_id?
> 
> This is the index action in the users controller:  
> def index  
> @user = User.find(:all, :conditions =\> { :id =\> :user\_id })  
> end  
> Either i’m horribly confused or you just need to do params[:user\_id].  
> Typically though you’d just do

\<%= link\_to user.screen\_name, {:action =\> “index”, :controller =\>  
“users”, :id =\> user.id }

and access it via params[:id]

Fred

---

<div class="post-metadata">

**Author:** ![TTDaVeTT](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/91b2a8/32.png) [@TTDaVeTT](https://www.ruby-forum.com/u/TTDaVeTT)\
**Post date:** [December 6, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/passing-parameters-through-link-to/122292/3 "2007-12-06T16:42:55Z")

</div>

It sounds like to me that you just want to be able to have a user  
profile page with that particular user’s info listed. Change your  
loop to this:

\<% for user in @users -%\>  
\<%= link\_to %Q{#{user.screen\_name}}, {:controller =\>  
“users”, :action =\> “index”, :id =\> user.id } %\>  
\<% end %\>

Then, what you need to do is create another action in your ‘users’  
controller, for example, ‘profile’ that finds the params/attributes  
for that user record.

Add this to your ‘users’ controller:

def profile  
@user = User.find(params[:id])  
end

Then, create a view called profile.rhtml. Once you do that you can  
call upon any of the user’s attributes with \<%= @user.method\_name %\>  
(e.g. @user.screen\_name, @user.email, etc.).
