Forum: Ruby on Rails RE: Re: Re: Auto refreshing a page based on select change

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Berger, Daniel (Guest)
on 2006-01-09 16:27
(Received via mailing list)
>
> 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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-09 18:15
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.
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Daniel Berger (Guest)
on 2006-01-09 19:08
(Received via mailing list)
Kevin Olbrich 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:

<!-- 'dates' are pregenerated -->
<%=
    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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-09 19:37
Daniel Berger wrote:
> Kevin Olbrich 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:
>
> <!-- 'dates' are pregenerated -->
> <%=
>     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.
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Daniel Berger (Guest)
on 2006-01-11 21:48
(Received via mailing list)
Kevin Olbrich 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:

<p><label for=schedule_start_date">Install Date</label<br/>
<%=
    select(:schedule, :start_date, dates, {:include_blank=>true},
       {:onchange => url_for(:controller=>"schedule", :action=>"new")}
    )
%>

<p><label for=schedule_slot_id">Slot Number</label<br/>
<%=
    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:

<select id="schedule_start_date" name="schedule[start_date]"
onchange="/schedule/new">

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

Thanks,

Dan
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-11 23:14
Daniel Berger 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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-12 01:40
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
This topic is locked and can not be replied to.