Hi all, I have a select helper form on a web page which contains a list of dates. When the user selects a date, one of the other forms on the page needs to be updated based on the newly selected date. Is there a way to catch this event in Rails? Are we talking Ajax here? Or is there a simpler way? Thanks, Dan
Kevin Olbrich
2006-Jan-06 20:23 UTC
[Rails] Re: Auto refreshing a page based on select change
Daniel Berger wrote:> Hi all, > > I have a select helper form on a web page which contains a list of > dates. When > the user selects a date, one of the other forms on the page needs to be > updated > based on the newly selected date. > > Is there a way to catch this event in Rails? Are we talking Ajax here? > Or is > there a simpler way? > > Thanks, > > DanYou could use a ''onchange()'' script with your select dropdown and postback a parameter that updates the other field. Not too difficult, but not as slick as an AJAX solution. -- Posted via http://www.ruby-forum.com/.
Daniel Berger
2006-Jan-06 21:50 UTC
[Rails] Re: Auto refreshing a page based on select change
Kevin Olbrich wrote:> Daniel Berger wrote: > >>Hi all, >> >>I have a select helper form on a web page which contains a list of >>dates. When >>the user selects a date, one of the other forms on the page needs to be >>updated >>based on the newly selected date. >> >>Is there a way to catch this event in Rails? Are we talking Ajax here? >>Or is >>there a simpler way? >> >>Thanks, >> >>Dan > > > You could use a ''onchange()'' script with your select dropdown and > postback a parameter that updates the other field. > > Not too difficult, but not as slick as an AJAX solution.Thanks Kevin, but I''m not sure how that should look exactly. <%select(:var, :attr, choices, :options => {:onchange => <create new select here> }) %> I''m having trouble following the docs. Please help. Thanks, Dan
Kevin Olbrich
2006-Jan-06 22:24 UTC
[Rails] Re: Re: Auto refreshing a page based on select change
Take a look at this.. http://www.faqs.org/docs/htmltut/forms/_SELECT_onChange.html -- Posted via http://www.ruby-forum.com/.
Berger, Daniel
2006-Jan-09 15:24 UTC
[Rails] Re: Re: Auto refreshing a page based on select change
> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Kevin Olbrich > Sent: Friday, January 06, 2006 3:25 PM > To: rails@lists.rubyonrails.org > Subject: [Rails] Re: Re: Auto refreshing a page based on select change > > > Take a look at this.. > > http://www.faqs.org/docs/htmltut/forms/_SELECT_onChange.htmlThanks 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
Kevin Olbrich
2006-Jan-09 17:15 UTC
[Rails] RE: Re: Re: Auto refreshing a page based on select change
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, > > DanTry 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. -- Posted via http://www.ruby-forum.com/.
Daniel Berger
2006-Jan-09 18:06 UTC
[Rails] RE: Re: Re: Auto refreshing a page based on select change
Kevin Olbrich wrote:> 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. > >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
Kevin Olbrich
2006-Jan-09 18:37 UTC
[Rails] Re: RE: Re: Re: Auto refreshing a page based on select chang
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, > > DanI 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. -- Posted via http://www.ruby-forum.com/.
Daniel Berger
2006-Jan-11 20:27 UTC
[Rails] RE: Re: Re: Auto refreshing a page based on select change
Kevin Olbrich wrote:> 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. > >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
Kevin Olbrich
2006-Jan-11 22:14 UTC
[Rails] Re: RE: Re: Re: Auto refreshing a page based on select chang
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 -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jan-12 00:40 UTC
[Rails] Re: RE: Re: Re: Auto refreshing a page based on select chang
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 -- Posted via http://www.ruby-forum.com/.
Vivek Krishna
2006-Jan-12 04:07 UTC
[Rails] Re: Auto refreshing a page based on select change
On 1/7/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:> > Daniel Berger wrote: > > Hi all, > > > > I have a select helper form on a web page which contains a list of > > dates. When > > the user selects a date, one of the other forms on the page needs to be > > updated > > based on the newly selected date. > > > > Is there a way to catch this event in Rails? Are we talking Ajax here? > > Or is > > there a simpler way? > > > > Thanks, > > > > Dan > > You could use a ''onchange()'' script with your select dropdown and > postback a parameter that updates the other field.Why cant you do Ajax.updater () for the onchange() event? That would be AJAX right ? There is an example at http://www.roryhansen.ca/?p=9 which uses the observer but I was thinking why not use the onchange insted of the observer? Not too difficult, but not as slick as an AJAX solution.> > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060112/66b1a168/attachment.html
Matthew
2006-Jan-16 14:06 UTC
[Rails] Re: Re: Auto refreshing a page based on select change
Thanks Kewin!! Your link:> http://www.faqs.org/docs/htmltut/forms/_SELECT_onChange.htmlhelped me solve a problem that''s I''ve been trying to fix since before christmas. Nice one. -- Posted via http://www.ruby-forum.com/.