# How to embed variables in a link\_to

**URL:** <https://www.ruby-forum.com/t/how-to-embed-variables-in-a-link-to/129336>\
**Category:** Rails\
**Created:** [February 17, 2008, 1:07am UTC](https://www.ruby-forum.com/t/how-to-embed-variables-in-a-link-to/129336 "2008-02-17T01:07:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 17, 2008, 1:07am UTC](https://www.ruby-forum.com/t/how-to-embed-variables-in-a-link-to/129336/1 "2008-02-17T01:07:10Z")

</div>

Hello,  
I’m just trying to figure out how to put a variable into a link\_to. I  
want to have the number of rows from my SQL statement in it. For  
example:

perfect matches (123)

where 123 is the number of rows. I currently have the number of rows in  
an array, but it just displays rows[pm\_rows] (when I put rows[‘pm\_rows’]  
it kicks a compile error due to the quotes) because its in  
quotations… how can I escape this? My link\_to looks like this:

\<%= link\_to ‘perfect matches (rows[pm\_rows])’, :controller =\> ‘compare’,  
:action =\> ‘perfectmatches’ %\>

Any help is appreciated!

Thank you!

- Jeff M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 17, 2008, 5:01am UTC](https://www.ruby-forum.com/t/how-to-embed-variables-in-a-link-to/129336/2 "2008-02-17T05:01:57Z")

</div>

Jeff M. wrote:

> Hello,  
> I’m just trying to figure out how to put a variable into a link\_to. I  
> want to have the number of rows from my SQL statement in it. For  
> example:
> 
> perfect matches (123)
> 
> where 123 is the number of rows. I currently have the number of rows in  
> an array, but it just displays rows[pm\_rows] (when I put rows[‘pm\_rows’]  
> it kicks a compile error due to the quotes) because its in  
> quotations… how can I escape this? My link\_to looks like this:
> 
> \<%= link\_to ‘perfect matches (rows[pm\_rows])’, :controller =\> ‘compare’,  
> :action =\> ‘perfectmatches’ %\>
> 
> Any help is appreciated!
> 
> Thank you!
> 
> - Jeff M.

Hi Jeff,  
You don’t send the parameters as if it was a function call. Instead,  
you set some arbitrary hash key to the value in question in the URL part  
of the link\_to, and then inside the link’ed to action you access it  
through the “params” with the key you gave it:

\<%= link\_to ‘perfect matches’,  
:url =\> {:controller =\> ‘compare’,  
:action =\> ‘perfectmatches’,  
:my\_rows =\> rows[pm\_rows]}  
%\>

def perfectmatches  
my\_rows = params[:my\_rows]  
…  
end

HTH,  
jp

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 17, 2008, 8:39am UTC](https://www.ruby-forum.com/t/how-to-embed-variables-in-a-link-to/129336/3 "2008-02-17T08:39:43Z")

</div>

“#{@myvar}”

[Http://www.rubyplus.org](Http://www.rubyplus.org)  
Free Ruby & Rails screencasts

On Feb 16, 2008, at 4:07 PM, Jeff M.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 18, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/how-to-embed-variables-in-a-link-to/129336/4 "2008-02-18T18:22:50Z")

</div>

Hello,  
Thanks for your response! The thing is, I’m trying to get this variable  
into the link text, not trying to send it somewhere. My controller comes  
up with the number of rows and I want to put it in the link text so it  
would read something like “perfect matches (200)” where 200 would be  
rows[‘pm\_rows’]… The quotes just kill it each time though… Any  
suggestions?

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 18, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/how-to-embed-variables-in-a-link-to/129336/5 "2008-02-18T22:07:35Z")

</div>

I got it to work, thanks!

\<% for rows in @pm\_rows %\>  
\<%= link\_to “perfect matches (#{rows[‘pmrows’]})”, :controller =\>  
‘compare’, :action =\> ‘index’ %\>  
\<% end %\>
