# How to use "link\_to" to deliver a parameter?

**URL:** <https://www.ruby-forum.com/t/how-to-use-link-to-to-deliver-a-parameter/124951>\
**Category:** Rails\
**Created:** [January 4, 2008, 12:11am UTC](https://www.ruby-forum.com/t/how-to-use-link-to-to-deliver-a-parameter/124951 "2008-01-04T00:11:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 4, 2008, 12:11am UTC](https://www.ruby-forum.com/t/how-to-use-link-to-to-deliver-a-parameter/124951/1 "2008-01-04T00:11:59Z")

</div>

Hello, there:  
I wish A link to invoke such a action together with the parameter  
“zh\_CN” “cookies/switch\_language/zh\_CH” to switch language  
sellection, and I use the following code:  
\<%= link\_to ‘Chinese’, :controller =\> ‘cookies’, :action =\>  
‘switch\_lang’ %\>

But, how is “link\_to” able to convey the params: zh\_CN as well?  
seems like link\_to doesn’t support a option :params =\>  
‘zh\_CN’…because it errors when I use  
'\<%= link\_to ‘Chinese’, :controller =\> ‘cookies’, :action =\>  
‘switch\_lang/zh\_CN’ %\> &  
\<%= link\_to ‘Chinese’, :controller =\> ‘cookies’, :action =\>  
‘switch\_lang’, params: =\> 'zh\_CN ’ %\>

Thanks in advance!  
Myst

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 4, 2008, 12:33am UTC](https://www.ruby-forum.com/t/how-to-use-link-to-to-deliver-a-parameter/124951/2 "2008-01-04T00:33:38Z")

</div>

> '\<%= link\_to ‘Chinese’, :controller =\> ‘cookies’, :action =\>  
> ‘switch\_lang/zh\_CN’ %\> &  
> \<%= link\_to ‘Chinese’, :controller =\> ‘cookies’, :action =\>  
> ‘switch\_lang’, params: =\> 'zh\_CN ’ %\>

Assuming you still have the default route installed use this:

\<%= link\_to ‘Chinese’, :controller =\> ‘cookies’,  
:action =\> ‘switch\_lang’,  
:id =\> ‘zh\_CN’ %\>

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 4, 2008, 6:37am UTC](https://www.ruby-forum.com/t/how-to-use-link-to-to-deliver-a-parameter/124951/3 "2008-01-04T06:37:15Z")

</div>

You could also use a custom route so you can use the symbol of your  
choosing and assemble the URL however you want. Something like…

\<%= link\_to ‘Chinese’, :controller =\> ‘cookies’,  
:action =\> ‘switch\_lang’,  
:lang =\> ‘zh\_CN’ %\>

…and…

map.connect ‘foo/:lang’, :controller =\> ‘cookies’, :action =\>  
‘switch\_lang’

Preston

–  
[PrestonLee.com](http://PrestonLee.com)  
OpenRain, LLC

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 4, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/how-to-use-link-to-to-deliver-a-parameter/124951/4 "2008-01-04T13:07:36Z")

</div>

Cool, thank you both!
