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


#1

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


#2

Berger, Daniel wrote:

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.


#3

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


#4

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.


#5

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


#6

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


#7

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