# Rails link\_to function passing arguments

**URL:** <https://www.ruby-forum.com/t/rails-link-to-function-passing-arguments/116771>\
**Category:** Rails\
**Created:** [October 9, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/rails-link-to-function-passing-arguments/116771 "2007-10-09T17:25:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![slackeast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@slackeast](https://www.ruby-forum.com/u/slackeast)\
**Post date:** [October 9, 2007, 5:25pm UTC](https://www.ruby-forum.com/t/rails-link-to-function-passing-arguments/116771/1 "2007-10-09T17:25:19Z")

</div>

I basically want to pass my own custom variables to the link\_to function  
so I don’t need to set up a bunch of actions within my controller.

So here’s more of the details:

link\_to example:

Code:

\<% for g in @games %\>  
\<%= link\_to "game " + g.game\_number.to\_s, :action =\> “gametemplate”%\>  
\<% end %\>

controller example code:  
Code:

def gametemplate  
b = Batter.new()  
@batter\_stats = b.find\_game\_stats 1, 2007, “winter”  
end

I can then pass the @batter\_stats to my view. However, how do I get the:  
Code:  
1, 2007, “winter”  
arguments from my link\_to function???

I can’t find an answer anywhere I feel this would be something easy to  
do in rails. I got around this by setting up a separate action and  
resulting view rhtml file to for each game. This is obviously not a very  
good solution especially if I have 50 games. I’m a newbie @ rails and  
any help would be appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![slackeast](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@slackeast](https://www.ruby-forum.com/u/slackeast)\
**Post date:** [October 9, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/rails-link-to-function-passing-arguments/116771/2 "2007-10-09T17:38:24Z")

</div>

On 9 Oct 2007, at 16:25, Jonathon Eastman wrote:

> I can then pass the @batter\_stats to my view. However, how do I get  
> the:  
> Code:  
> 1, 2007, “winter”  
> arguments from my link\_to function???

You can pass as many arguments as you want:  
\<%= link\_to "game " + g.game\_number.to\_s, :action =\>  
“gametemplate”, :id =\> 1, :season =\> ‘winter’, :year =\> 2007%\>  
These will be accessible from the controller as params[:id], params  
[:year] etc…

Fred
