# RE: Re: Re: Auto refreshing a page based on select change

**URL:** <https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951>\
**Category:** Rails\
**Created:** [January 9, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951 "2006-01-09T16:27:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [January 9, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951/1 "2006-01-09T16:27:09Z")

</div>

> [HTML onChange - HTML Code Tutorial](http://www.faqs.org/docs/htmltut/forms/_SELECT_onChange.html)

Thanks Kevin, but I’m still confused as to how this should look on the  
Ruby/Rails side of the house, i.e within the .rhtml file.

Can you please provide an example?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [January 9, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951/2 "2006-01-09T18:15:25Z")

</div>

Berger, Daniel wrote:

> > [HTML onChange - HTML Code Tutorial](http://www.faqs.org/docs/htmltut/forms/_SELECT_onChange.html)
> 
> Thanks Kevin, but I’m still confused as to how this should look on the  
> Ruby/Rails side of the house, i.e within the .rhtml file.
> 
> Can you please provide an example?
> 
> Thanks,
> 
> Dan

Try putting this in a rhtml file with the appropriate substitutions for  
item.

\<%= select ‘item’, ‘item\_id’, items, {“onchange”=\>“ONCHANGE=‘location =  
this.options[this.selectedIndex].value;’”} %\>

You might need to tweak the location in the script a bit… I’m pretty  
sure the one I’m using calls url\_for(), but I’m not in front of my  
development box ATM.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [January 9, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951/3 "2006-01-09T19:08:15Z")

</div>

Kevin O. wrote:

> > 
> 
> sure the one I’m using calls url\_for(), but I’m not in front of my  
> development box ATM.

Hi Kevin,

Where I’m lost is how to get the selected value into a Ruby variable.  
Let me  
provide a more concrete code sample:

\<%=  
select(:schedule, :date\_scheduled, dates)  
%\>

\<%=  
select(:schedule, :slot\_id, date\_selected)  
%\>

How do I set ‘date\_selected’ in the second select list based on the date  
they  
chose from the first select list?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [January 9, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951/4 "2006-01-09T19:37:42Z")

</div>

Daniel B. wrote:

> Kevin O. wrote:
> 
> > > 
> > 
> > sure the one I’m using calls url\_for(), but I’m not in front of my  
> > development box ATM.
> 
> Hi Kevin,
> 
> Where I’m lost is how to get the selected value into a Ruby variable.  
> Let me  
> provide a more concrete code sample:
> 
> \<%=  
> select(:schedule, :date\_scheduled, dates)  
> %\>
> 
> \<%=  
> select(:schedule, :slot\_id, date\_selected)  
> %\>
> 
> How do I set ‘date\_selected’ in the second select list based on the date  
> they  
> chose from the first select list?
> 
> Thanks,
> 
> Dan

I see.

If you append text like “?date\_scheduled=1” to the URL in the onchange  
it will post back to the current URL and set the params[:date\_scheduled]  
to “1”

something like this…

==controller==

def action  
@schedule = Schedule.find(params[:id]) #load the object  
@date\_scheduled = params[:date\_scheduled] || “1” #pick a default incase  
none  
@schedule.date\_scheduled = @date\_scheduled  
@schedule.slot\_id = @date\_scheduled  
@date\_list = Schedule.dates.find(:all)  
@slot\_list = Schedule.slots.find\_all\_by\_date\_id(@date\_scheduled)  
end

==action.rhtml==

\<%= select ‘schedule’, ‘date\_scheduled’, @date\_list,  
{“onchange”=\>“location = ‘#{url\_for()}?date\_submitted=’+  
this.options[this.selectedIndex].value;”} %\>

\<%= select ‘schedule’, ‘slot\_id’, @slot\_list %\>

==================

The first select initates a post back to the current controller action  
when changed.

You can even change the contents of the second drop down by changing the  
contents of @slot\_list based on the submitted date\_submitted.

There are AJAXy ways of doing this, but this works.

It probably needs to be tweaked for your model since I have no idea what  
associations you are using.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [January 11, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951/5 "2006-01-11T23:14:11Z")

</div>

Daniel B. wrote:

> \<%=  
> select(:schedule, :start\_date, dates, {:include\_blank=\>true},  
> {:onchange =\> url\_for(:controller=\>“schedule”, :action=\>“new”)}  
> )  
> %\>  
> Why am I not getting a full URL there in the ‘onchange’ portion? Or  
> can’t I do  
> it like that?

The onchange is definitely not right. I’ll pull a working one off my  
dev machine later tonight and post it for you.

\_Kevin

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [January 12, 2006, 1:40am UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951/6 "2006-01-12T01:40:41Z")

</div>

This select tag will re-submit on a change

select\_tag “Name”, options\_for\_select(collection, selected ),  
:onchange=\>“location =  
‘#{url\_for()}?sort=’+this.option[this.selectedIndex].value”

\_Kevin

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [January 11, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/re-re-re-auto-refreshing-a-page-based-on-select-change/51951/7 "2006-01-11T21:48:06Z")

</div>

Kevin O. wrote:

> > 
> 
> sure the one I’m using calls url\_for(), but I’m not in front of my  
> development box ATM.

Alright, I think I’m getting closer. The action is Schedule.new, and  
that’s  
where I set @available\_slots. In the view, I have this:

Install Date\</label  
\<%= select(:schedule, :start\_date, dates, {:include\_blank=\>true}, {:onchange =\> url\_for(:controller=\>"schedule", :action=\>"new")} ) %\>

Slot Number\</label  
\<%= select(:schedule, :slot\_id, @available\_slots) %\>

But something’s not quite right, as it’s not reloading the page (and  
thus not  
resetting @available slots). When I view the source for the page, the  
first  
part of the HTML for the selection list looks like this:

Why am I not getting a full URL there in the ‘onchange’ portion? Or  
can’t I do  
it like that?

Thanks,

Dan
