# Select box onchange problem

**URL:** <https://www.ruby-forum.com/t/select-box-onchange-problem/63333>\
**Category:** Rails\
**Created:** [June 14, 2006, 10:08am UTC](https://www.ruby-forum.com/t/select-box-onchange-problem/63333 "2006-06-14T10:08:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 14, 2006, 10:08am UTC](https://www.ruby-forum.com/t/select-box-onchange-problem/63333/1 "2006-06-14T10:08:27Z")

</div>

Hi,

i’ve got the following problem. I have a check\_box witch shows  
a list of varieties. Now i want to call a action when the user  
changes his selection. (the method to be called, will store the  
selection from the user).

This is my code:

\<%= select(‘variety’, ‘variety\_id’,  
Variety.find\_all.collect {|c| [c.description, c.id]},  
{},  
{“onchange” =\>  
url\_for(:controller =\> ‘account’,  
:action =\> ‘add\_to\_varietytmp’)} ) %\>

HTML output:

Two questions:

1… I need to give a id to the method ‘add\_to\_varietytmp’, how do  
I give the correct id from the selected option?

2… When I select an option in my selectbox, onchange, the method  
‘add\_to\_varietytmp’ doesn’t run. So how do I get my onchange  
to work?

Thnx  
micheldogger

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 14, 2006, 10:23am UTC](https://www.ruby-forum.com/t/select-box-onchange-problem/63333/2 "2006-06-14T10:23:56Z")

</div>

Hi Michel,

#1: just add :id =\> c.id to url\_for, assuming c.id is the id of the  
selected  
option  
#2: When you say, “onchange=’/some/url’” you’re not actually _doing_  
anything. You have to assign some javascript to onchange for something  
to  
happen. In this case I’m not sure how to use Rails to generate the  
javascript for you, but you’re going to want to use Prototype’s  
Ajax.Request  
or Ajax.Updater functions.

The result will be something like:

I hope this is a helpful starting point!

Daniel

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 14, 2006, 11:28am UTC](https://www.ruby-forum.com/t/select-box-onchange-problem/63333/3 "2006-06-14T11:28:26Z")

</div>

Daniel,

i’ve been looking for it. I found a quick 'n dirty solution  
but i keep struggling with the id to pass to my action.

This is the solution I found:

\<%= select(‘variety’, ‘variety\_id’, Variety.find\_all.collect {|c|  
[c.description, c.id]},{:id =\> ‘variety\_select’},  
{ “onchange” =\>  
“document.location.href=’/account/add\_to\_varietytmp/  
#{c.id}’”}) %\>

I tried to pass the id with c.id, but that doesn’t work. (look above)  
I get the following error:

undefined local variable or method `c’ for  
#\<#Class:0x3807048:0x3807018\>

First I’ll need a quick solution, even if it isn’t that nice.  
My problem now is how to get the value from the selected option.

thnx,  
micheldogger

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 14, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/select-box-onchange-problem/63333/4 "2006-06-14T18:21:35Z")

</div>

I didn’t catch this earlier, but part of the problem is that when you  
use  
c.id, it’s out of scope. Try  
“document.location.href=’/account/add\_to\_varitetytmp/’+this.value” ,  
which  
will use javascript to set the id part of the path based on the option  
selected

---

<div class="post-metadata">

**Author:** ![micheldogger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b5e925/32.png) [@micheldogger](https://www.ruby-forum.com/u/micheldogger)\
**Post date:** [June 14, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/select-box-onchange-problem/63333/5 "2006-06-14T19:31:32Z")

</div>

Daniel,

Thank you. IT WORKS!!!

So for everybody who might want to use this in the future:

- Table: Countries  
id integer  
name varchar(45)

- Below, the code for a select menu in a view, wich shows all the  
countries and if you select a country, the onchange action will  
call a controller and action together with the id of the selected  
country:

\<%= select(  
‘country’, ‘country\_id’,  
Country.find\_all.collect {|c| [c.name, c.id]},{},  
{“onchange” =\>  
“document.location.href=’/controller/action/’+this.value”}  
)%\>

micheldogger
