# How to pass 2 variables to controller from \<a href="#" onCli

**URL:** <https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685>\
**Category:** Rails\
**Created:** [October 8, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685 "2007-10-08T19:06:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![narozk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d78d45/32.png) [@narozk](https://www.ruby-forum.com/u/narozk)\
**Post date:** [October 8, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685/1 "2007-10-08T19:06:24Z")

</div>

I want to pass 2 variables (@variableA, @variableB) to my controller  
“search” when the following href is clicked:

myView.rhtml:

- **HOTEL**

I need the 2 variables for my condition string, which should work  
something like this:

conditions = [“hotel\_id = ? AND city\_id = ?”, @variableA, @variableB]  
unless @hotel\_id.nil?

---

<div class="post-metadata">

**Author:** ![narozk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d78d45/32.png) [@narozk](https://www.ruby-forum.com/u/narozk)\
**Post date:** [October 8, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685/2 "2007-10-08T19:12:43Z")

</div>

On 8 Oct 2007, at 18:06, Zoran K. wrote:

link\_to\_remote ‘Hotel’, :url =\> {:action =\> ‘search’, :a =\>  
‘123’, :b =\> ‘456’},  
:before =\> “Element.show(‘spinner’)”,  
:success=\> “Element.hide(‘spinner’)”

---

<div class="post-metadata">

**Author:** ![narozk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d78d45/32.png) [@narozk](https://www.ruby-forum.com/u/narozk)\
**Post date:** [October 8, 2007, 7:16pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685/3 "2007-10-08T19:16:12Z")

</div>

Zoran K. wrote:

> myView.rhtml:
> 
> - **HOTEL**

Are you hard coding the link like that? Because you are using a help you  
could say:

\<%= link\_to ‘Home’, { :action =\> ‘home\_form’, :id =\> ‘arg\_1’, :id2 =\>  
‘arg\_2’ } %\>

---

<div class="post-metadata">

**Author:** ![narozk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d78d45/32.png) [@narozk](https://www.ruby-forum.com/u/narozk)\
**Post date:** [October 8, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685/4 "2007-10-08T19:32:47Z")

</div>

> Are you hard coding the link like that? Because you are using a help you  
> could say:
> 
> \<%= link\_to ‘Home’, { :action =\> ‘home\_form’, :id =\> ‘arg\_1’, :id2 =\>  
> ‘arg\_2’ } %\>

yes, it’s hard coded because of my quite extensive stylesheet (a, b, ul,  
li…)

---

<div class="post-metadata">

**Author:** ![narozk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d78d45/32.png) [@narozk](https://www.ruby-forum.com/u/narozk)\
**Post date:** [October 8, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685/5 "2007-10-08T20:41:58Z")

</div>

Thank you guys,

this looks ugly but it works now:

**HOTEL**

---

<div class="post-metadata">

**Author:** ![narozk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d78d45/32.png) [@narozk](https://www.ruby-forum.com/u/narozk)\
**Post date:** [October 8, 2007, 10:17pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685/6 "2007-10-08T22:17:14Z")

</div>

a better solution:

\<%= link\_to\_remote “ **HOME** ”, { :url =\> { :controller =\> “search”,  
:action =\> “index”, :arg1 =\> “1”, :arg2 =\> “2” }, :update =\>  
“resultsTable”, :success =\> “Element.hide(‘spinner’)” } %\>

link\_to\_remote “ **HOME** ” is certainly not perfect, but I can live  
with this 🙂

---

<div class="post-metadata">

**Author:** ![narozk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d78d45/32.png) [@narozk](https://www.ruby-forum.com/u/narozk)\
**Post date:** [October 10, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/how-to-pass-2-variables-to-controller-from-a-href-oncli/116685/7 "2007-10-10T19:15:52Z")

</div>

Zoran,

Since you’re hard-coding all of this, an even better solution would be  
to use a named route. You add named routes in the config/routes.rb  
file. Search for ‘named route’ and it’ll give you an example (around  
line 10 of a vanilla routes.rb).

In your case you’d probably add something like this:

map.connect  
‘home’, :controller=\>‘search’, :action=\>‘index’, :arg1=\>1, :arg2=\>

Then your method call is reduced to:

link\_to\_remote “**Home\</  
b\>”, :url=\>home\_url, :update=\>‘resultsTable’,  
:sucess=\>'Element.hide(‘spinner’)**

 

As an observation, you might want to also change :success  
to :complete. That way the spinner gets hidden even with an error  
response (success only triggers with a 200 response).

On Oct 8, 4:17 pm, Zoran K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
