Hello. I want to select an item from a select list and have that result in an ajax call that updates two sections on the page. Here is the code in the controller def create ... render :update do |page| page.replace_html ''list_items'', :partial => ''lists/list_items'' page.replace_html ''add_item'', :partial => ''lists/add_item'' end end Here is the version of code in the template that I WANT to use to POST to this action: <%= form_remote_tag :html => { :action => url_for(:controller => "list_item_links", :action => "create") }, :eval_scripts => true %> <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> <%= hidden_field ''list_item_link'', ''list_item_type'', :value => ''List'' %> Which list? <%= select ''list_item_link'', ''list_item_id'', @lists.collect{ |l| [ l.name, l.id] }, { :include_blank => true}, { :class => "formfield", :onchange => "this.form.submit()" } %> <%= end_form_tag %> And here is the code in the template that actually works (using a GET instead): Which list? <%= select ''list_item_link'', ''list_item_id'', @lists.collect{ |l| [ l.name, l.id] }, { :include_blank => true}, { :class => "formfield", :onchange => "new Ajax.Request(''/admin/list_item_links/create?list_item_link [list_item_type]=List&list_item_link[list_id]=#{@list.id} &list_item_link[list_item_id]='' + this.options[selectedIndex].value,{asynchronous:true, evalScripts:true}); return false;" } %> The first example, using the form, results in javascript being rendered in the browser rather than being executed. Note that it does say ":eval_scripts => true" and that the resulting javascript does include "evalScripts:true". I''m using rails 1.1.2. Thanks in advance, David
It occurs to me that this example would be easier to grok - the other example is adding a list to another list (we''ve got lists of anything, including other lists :) ). Here''s the same example only the goal is to add a story to a list: DESIRED (but doesn''t work) <%= form_remote_tag :html => { :action => url_for(:controller => "list_item_links", :action => "create") }, :eval_scripts => true%> <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> <%= hidden_field ''list_item_link'', ''list_item_type'', :value => ''Story'' %> Which story? <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ |l| [ l.name, l.id] }, { :include_blank => true}, { :class => "formfield", :onchange => "this.form.submit()" } %> <%= end_form_tag %> WORKING (but not desired) Which story? <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ |l| [ l.name, l.id] }, { :include_blank => true}, { :class => "formfield", :onchange => "new Ajax.Request(''/admin/list_item_links/create?list_item_link [list_item_type]=Story&list_item_link[list_id]=#{@list.id} &list_item_link[list_item_id]='' + this.options[selectedIndex].value,{asynchronous:true, evalScripts:true}); return false;" } %> Thanks again, David On Jul 2, 2006, at 5:17 PM, David Chelimsky wrote:> Hello. I want to select an item from a select list and have that > result in an ajax call that updates two sections on the page. Here > is the code in the controller > > def create > ... > render :update do |page| > page.replace_html ''list_items'', :partial => ''lists/list_items'' > page.replace_html ''add_item'', :partial => ''lists/add_item'' > end > end > > Here is the version of code in the template that I WANT to use to > POST to this action: > > <%= form_remote_tag :html => { :action => url_for(:controller => > "list_item_links", :action => "create") }, :eval_scripts => true %> > <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> > <%= hidden_field ''list_item_link'', ''list_item_type'', :value => > ''List'' %> > Which list? > <%= select ''list_item_link'', ''list_item_id'', @lists.collect{ |l| > [ l.name, l.id] }, > { :include_blank => true}, > { :class => "formfield", :onchange => "this.form.submit()" } > %> > <%= end_form_tag %> > > And here is the code in the template that actually works (using a > GET instead): > > Which list? > <%= select ''list_item_link'', ''list_item_id'', @lists.collect{ |l| > [ l.name, l.id] }, > { :include_blank => true}, > { :class => "formfield", :onchange => "new > Ajax.Request(''/admin/list_item_links/create?list_item_link > [list_item_type]=List&list_item_link[list_id]=#{@list.id} > &list_item_link[list_item_id]='' + > this.options[selectedIndex].value,{asynchronous:true, > evalScripts:true}); return false;" } > %> > > The first example, using the form, results in javascript being > rendered in the browser rather than being executed. Note that it > does say ":eval_scripts => true" and that the resulting javascript > does include "evalScripts:true". > > I''m using rails 1.1.2. > > Thanks in advance, > David > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Jul 2, 2006, at 3:55 PM, David Chelimsky wrote:> It occurs to me that this example would be easier to grok - the > other example is adding a list to another list (we''ve got lists of > anything, including other lists :) ). Here''s the same example only > the goal is to add a story to a list: > > DESIRED (but doesn''t work) > <%= form_remote_tag :html => { :action => url_for(:controller => > "list_item_links", :action => "create") }, :eval_scripts => true%> > <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> > <%= hidden_field ''list_item_link'', ''list_item_type'', :value => > ''Story'' %> > Which story? > <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ |l| > [ l.name, l.id] }, > { :include_blank => true}, > { :class => "formfield", :onchange => "this.form.submit()" } > %> > <%= end_form_tag %> > > WORKING (but not desired) > Which story? > <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ |l| > [ l.name, l.id] }, > { :include_blank => true}, > { :class => "formfield", :onchange => "new > Ajax.Request(''/admin/list_item_links/create?list_item_link > [list_item_type]=Story&list_item_link[list_id]=#{@list.id} > &list_item_link[list_item_id]='' + > this.options[selectedIndex].value,{asynchronous:true, > evalScripts:true}); return false;" } > %> > > Thanks again, > DavidHow are you handling the controller side of the desired version? Are you using rjs? If you are using rjs then you don''t need eval_scripts. -Ezra
Ezra, Thanks for your reply. I''ve tried this with and without :eval_scripts, using an rjs file and also embedding the rjs in the controller method: def create ... render :update do |page| page.replace_html ''list_items'', :partial => ''lists/list_items'' page.replace_html ''add_item'', :partial => ''lists/add_item'' end end No matter which way I do it, it seems that the form tag approach results in javascript getting rendered in the browser, but the select list calling the remote function directly results in the same javascript being executed. Any other ideas? Can anyone post a working example of doing what I''m trying to do, which is to submit a form on selecting from a select list and then updating two sections on the page? Thanks, David On Jul 2, 2006, at 6:00 PM, Ezra Zygmuntowicz wrote:> > On Jul 2, 2006, at 3:55 PM, David Chelimsky wrote: > >> It occurs to me that this example would be easier to grok - the >> other example is adding a list to another list (we''ve got lists of >> anything, including other lists :) ). Here''s the same example only >> the goal is to add a story to a list: >> >> DESIRED (but doesn''t work) >> <%= form_remote_tag :html => { :action => url_for(:controller => >> "list_item_links", :action => "create") }, :eval_scripts => true%> >> <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> >> <%= hidden_field ''list_item_link'', ''list_item_type'', :value => >> ''Story'' %> >> Which story? >> <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ |l| >> [ l.name, l.id] }, >> { :include_blank => true}, >> { :class => "formfield", :onchange => "this.form.submit()" } >> %> >> <%= end_form_tag %> >> >> WORKING (but not desired) >> Which story? >> <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ |l| >> [ l.name, l.id] }, >> { :include_blank => true}, >> { :class => "formfield", :onchange => "new >> Ajax.Request(''/admin/list_item_links/create?list_item_link >> [list_item_type]=Story&list_item_link[list_id]=#{@list.id} >> &list_item_link[list_item_id]='' + >> this.options[selectedIndex].value,{asynchronous:true, >> evalScripts:true}); return false;" } >> %> >> >> Thanks again, >> David > > How are you handling the controller side of the desired version? > Are you using rjs? If you are using rjs then you don''t need > eval_scripts. > > -Ezra > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Well, I figured out the root of the problem. The resulting html is this: <form action="/admin/list_item_links/create" method="post" onsubmit="new Ajax.Request('''', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"> ... <select class="formfield" id="list_item_link_list_item_id" name="list_item_link[list_item_id]" onchange="this.form.submit()"> ... </select> </form> From what I can tell, when you submit the form using onchange="this.form.submit()" it bypasses the onsubmit event in the form tag. That make sense? So I have to play around w/ some other approach, like a remote_function that has access to the form. I''ll keep trying - but please let me know if anyone has a solution. Thanks, David On Jul 2, 2006, at 10:58 PM, David Chelimsky wrote:> Ezra, > > Thanks for your reply. > > I''ve tried this with and without :eval_scripts, using an rjs file > and also embedding the rjs in the controller method: > > def create > ... > render :update do |page| > page.replace_html ''list_items'', :partial => ''lists/list_items'' > page.replace_html ''add_item'', :partial => ''lists/add_item'' > end > end > > No matter which way I do it, it seems that the form tag approach > results in javascript getting rendered in the browser, but the > select list calling the remote function directly results in the > same javascript being executed. > > Any other ideas? Can anyone post a working example of doing what > I''m trying to do, which is to submit a form on selecting from a > select list and then updating two sections on the page? > > Thanks, > David > > > On Jul 2, 2006, at 6:00 PM, Ezra Zygmuntowicz wrote: > >> >> On Jul 2, 2006, at 3:55 PM, David Chelimsky wrote: >> >>> It occurs to me that this example would be easier to grok - the >>> other example is adding a list to another list (we''ve got lists >>> of anything, including other lists :) ). Here''s the same example >>> only the goal is to add a story to a list: >>> >>> DESIRED (but doesn''t work) >>> <%= form_remote_tag :html => { :action => url_for(:controller => >>> "list_item_links", :action => "create") }, :eval_scripts => true%> >>> <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> >>> <%= hidden_field ''list_item_link'', ''list_item_type'', :value => >>> ''Story'' %> >>> Which story? >>> <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ | >>> l| [ l.name, l.id] }, >>> { :include_blank => true}, >>> { :class => "formfield", :onchange => "this.form.submit()" } >>> %> >>> <%= end_form_tag %> >>> >>> WORKING (but not desired) >>> Which story? >>> <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ | >>> l| [ l.name, l.id] }, >>> { :include_blank => true}, >>> { :class => "formfield", :onchange => "new >>> Ajax.Request(''/admin/list_item_links/create?list_item_link >>> [list_item_type]=Story&list_item_link[list_id]=#{@list.id} >>> &list_item_link[list_item_id]='' + >>> this.options[selectedIndex].value,{asynchronous:true, >>> evalScripts:true}); return false;" } >>> %> >>> >>> Thanks again, >>> David >> >> How are you handling the controller side of the desired version? >> Are you using rjs? If you are using rjs then you don''t need >> eval_scripts. >> >> -Ezra >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Did you try with this.form.onsubmit() Stephan David Chelimsky wrote:> Well, I figured out the root of the problem. The resulting html is this: > > <form > action="/admin/list_item_links/create" > method="post" onsubmit="new Ajax.Request('''', {asynchronous:true, > evalScripts:true, parameters:Form.serialize(this)}); return false;"> > ... > <select class="formfield" id="list_item_link_list_item_id" > name="list_item_link[list_item_id]" onchange="this.form.submit()"> > ... > </select> > </form> > > From what I can tell, when you submit the form using > onchange="this.form.submit()" it bypasses the onsubmit event in the > form tag. That make sense? So I have to play around w/ some other > approach, like a remote_function that has access to the form. I''ll > keep trying - but please let me know if anyone has a solution. > > Thanks, > David-- Posted via http://www.ruby-forum.com/.
For anyone interested, here is a solution that seems to work: <%= form_tag %> <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> <%= hidden_field ''list_item_link'', ''list_item_type'', :value => ''Story'' %> Which story? <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ |list| [ list.name, list.id] }, { :include_blank => true}, { :class => "formfield", :onchange => remote_function( :url => url_for(:controller => ''list_item_links'', :action => ''create''), :with => ''Form.serialize(this.form)'' ) } %> <%= end_form_tag %> which produces this: <form action="/admin/list_item_links/select_story" method="post"> <input id="list_item_link_list_id" name="list_item_link[list_id]" type="hidden" value="2" /> <input id="list_item_link_list_item_type" name="list_item_link [list_item_type]" type="hidden" value="Story" /> Which story? <select class="formfield" id="list_item_link_list_item_id" name="list_item_link[list_item_id]" onchange="new Ajax.Request(''/ admin/list_item_links/create'', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form)})"><option value=""></option> <option value="2">Biography</option> <option value="3">Turning the Tide</option></select> </form> On Jul 2, 2006, at 11:32 PM, David Chelimsky wrote:> Well, I figured out the root of the problem. The resulting html is > this: > > <form > action="/admin/list_item_links/create" > method="post" onsubmit="new Ajax.Request('''', {asynchronous:true, > evalScripts:true, parameters:Form.serialize(this)}); return false;"> > ... > <select class="formfield" id="list_item_link_list_item_id" > name="list_item_link[list_item_id]" onchange="this.form.submit()"> > ... > </select> > </form> > > From what I can tell, when you submit the form using > onchange="this.form.submit()" it bypasses the onsubmit event in the > form tag. That make sense? So I have to play around w/ some other > approach, like a remote_function that has access to the form. I''ll > keep trying - but please let me know if anyone has a solution. > > Thanks, > David > > On Jul 2, 2006, at 10:58 PM, David Chelimsky wrote: > >> Ezra, >> >> Thanks for your reply. >> >> I''ve tried this with and without :eval_scripts, using an rjs file >> and also embedding the rjs in the controller method: >> >> def create >> ... >> render :update do |page| >> page.replace_html ''list_items'', :partial => ''lists/list_items'' >> page.replace_html ''add_item'', :partial => ''lists/add_item'' >> end >> end >> >> No matter which way I do it, it seems that the form tag approach >> results in javascript getting rendered in the browser, but the >> select list calling the remote function directly results in the >> same javascript being executed. >> >> Any other ideas? Can anyone post a working example of doing what >> I''m trying to do, which is to submit a form on selecting from a >> select list and then updating two sections on the page? >> >> Thanks, >> David >> >> >> On Jul 2, 2006, at 6:00 PM, Ezra Zygmuntowicz wrote: >> >>> >>> On Jul 2, 2006, at 3:55 PM, David Chelimsky wrote: >>> >>>> It occurs to me that this example would be easier to grok - the >>>> other example is adding a list to another list (we''ve got lists >>>> of anything, including other lists :) ). Here''s the same example >>>> only the goal is to add a story to a list: >>>> >>>> DESIRED (but doesn''t work) >>>> <%= form_remote_tag :html => { :action => url_for(:controller => >>>> "list_item_links", :action => "create") }, :eval_scripts => true%> >>>> <%= hidden_field ''list_item_link'', ''list_id'', :value => @list.id%> >>>> <%= hidden_field ''list_item_link'', ''list_item_type'', :value => >>>> ''Story'' %> >>>> Which story? >>>> <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ | >>>> l| [ l.name, l.id] }, >>>> { :include_blank => true}, >>>> { :class => "formfield", :onchange => "this.form.submit()" } >>>> %> >>>> <%= end_form_tag %> >>>> >>>> WORKING (but not desired) >>>> Which story? >>>> <%= select ''list_item_link'', ''list_item_id'', @stories.collect{ | >>>> l| [ l.name, l.id] }, >>>> { :include_blank => true}, >>>> { :class => "formfield", :onchange => "new >>>> Ajax.Request(''/admin/list_item_links/create?list_item_link >>>> [list_item_type]=Story&list_item_link[list_id]=#{@list.id} >>>> &list_item_link[list_item_id]='' + >>>> this.options[selectedIndex].value,{asynchronous:true, >>>> evalScripts:true}); return false;" } >>>> %> >>>> >>>> Thanks again, >>>> David >>> >>> How are you handling the controller side of the desired version? >>> Are you using rjs? If you are using rjs then you don''t need >>> eval_scripts. >>> >>> -Ezra >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails