Jennifer Maas
2008-Jan-17 17:09 UTC
Returned RJS not executing when I submit a form with onclick instead of a submit tag
I''m having a problem getting the rjs sent to a client from a controller to execute when I submit the form with an onclick event in a checkbox instead of using a submit tag. The returned js is rendered as text. I create a form with form_remote_tag <% form_remote_tag :html => {:action => ''add_to_filter'', :name => "frm_categories"} do %> I''ve got a bunch of checkboxes in the form, and I add a manually add an onclick event to each one that submits the form. When one of the check boxes is clicked, the form submits, and my controller processes everything and sends back the javascript, but it''s not being executed by the client, it''s just being displayed as text. The returned javascript is executed perfectly when I use a submit tag created by the submit_tag helper or manually (even when I manually create a submit tag with a name other than ''commit'', which is the default name that submit_tag generates). Why would using the onclick or onchange events be any different? What magic does the submit tag do that an onclick = "document.frm_categories.submit()" wouldn''t do? I guess if I can''t get this to work, I can create field observers for each of my twenty checkboxes or a form observer? Would that be a better route anyway? Thanks for any advice! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Jennifer Maas
2008-Jan-17 17:14 UTC
Re: Returned RJS not executing when I submit a form with onclick instead of a submit tag
One other detail, I detected that that the respond_to wants "html" when the onclick event is used to submit and "js" when I use the submit tag. That''s probably obvious though. On Jan 17, 9:09 am, Jennifer Maas <jenniferm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m having a problem getting the rjs sent to a client from a > controller to execute when I submit the form with an onclick event in > a checkbox instead of using a submit tag. The returned js is rendered > as text. > > I create a form with form_remote_tag <% form_remote_tag :html => > {:action => ''add_to_filter'', :name => "frm_categories"} do %> I''ve got > a bunch of checkboxes in the form, and I add a manually add an onclick > event to each one that submits the form. > > When one of the check boxes is clicked, the form submits, and my > controller processes everything and sends back the javascript, but > it''s not being executed by the client, it''s just being displayed as > text. > > The returned javascript is executed perfectly when I use a submit tag > created by the submit_tag helper or manually (even when I manually > create a submit tag with a name other than ''commit'', which is the > default name that submit_tag generates). Why would using the onclick > or onchange events be any different? What magic does the submit tag > do that an onclick = "document.frm_categories.submit()" wouldn''t do? > > I guess if I can''t get this to work, I can create field observers for > each of my twenty checkboxes or a form observer? Would that be a > better route anyway? > > Thanks for any advice!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Jan-17 18:04 UTC
Re: Returned RJS not executing when I submit a form with onclick instead of a submit tag
> On Jan 17, 9:09 am, Jennifer Maas <jenniferm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I''m having a problem getting the rjs sent to a client from a > > controller to execute when I submit the form with an onclick event in > > a checkbox instead of using a submit tag. The returned js is rendered > > as text.Probably whatever custom code you have written to monitor the onclick event of a checkbox is doing an Ajax request with evalScripts set to false. Can you post that code? Also, what version of Prototype are you using? -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Jennifer Maas
2008-Jan-17 19:39 UTC
Re: Returned RJS not executing when I submit a form with onclick instead of a submit tag
I am using prototype 1.5 *I think*. I put version 1.5 in my public javascript folder, so I assume that''s the version it''s picking up. Is there an easy way to tell? Here''s the code for the form_remote_tag: <% form_remote_tag :url => {:action => ''add_to_filter2''}, :html => {:name => "frm_categories"} do %> Here''s the code to create the checkboxes (clicking them returns unexecuted javascript): <%= check_box_tag replace_spaces(category.name),replace_spaces(category.name), false, :name => "category[#{category.name}]", :onclick => ''document.frm_categories.submit();'' %> Here''s the code for the submit tag (clicking it returns executed javascript: <%= submit_tag ''filter'' %> On Jan 17, 10:04 am, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On Jan 17, 9:09 am, Jennifer Maas <jenniferm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m having a problem getting the rjs sent to a client from a > > > controller to execute when I submit the form with an onclick event in > > > a checkbox instead of using a submit tag. The returned js is rendered > > > as text. > > Probably whatever custom code you have written to monitor the onclick > event of a checkbox is doing an Ajax request with evalScripts set to > false. Can you post that code? > > Also, what version of Prototype are you using? > > -justin--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Jan-17 20:11 UTC
Re: Returned RJS not executing when I submit a form with onclick instead of a submit tag
On Jan 17, 2008 1:39 PM, Jennifer Maas <jennifermaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am using prototype 1.5 *I think*. I put version 1.5 in my public > javascript folder, so I assume that''s the version it''s picking up. Is > there an easy way to tell?Just look at the prototype.js file, it''s right at the top. The latest version if 1.6.0, there are many changes from 1.5 to 1.6. Also there is a newer version of Scriptaculous (1.8) to go along with the new version of Prototype.> Here''s the code to create the checkboxes (clicking them returns > unexecuted javascript): > > <%= check_box_tag > replace_spaces(category.name),replace_spaces(category.name), false, > :name => "category[#{category.name}]", > :onclick => ''document.frm_categories.submit();'' %>When you call submit like that, you are submitting the form through the normal form POST means, not the Ajax way. Why not just have individual checkboxes that have their own Ajax request and do away with the remote_form? <%= check_box_tag replace_spaces(category.name),replace_spaces(category.name), false, :name => "category[#{category.name}]", :onclick => remote_function( :url => { :action => ''add_filter'' }, :with => "''#{ replace_spaces(category.name) }='' + this.value" %> I''m taking advantage of another Rails helper method, remote_function, which translates to a new Ajax request. See this page for more info: http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html#M000965 If you want to leave your code relatively untouched, and still use the remote_form combined with each click of a checkbox causing an complete form submit, try something like this: <% form_remote_tag :url => {:action => ''add_to_filter2''}, :html => {:id => "frm_categories"} do -%> <%= check_box_tag replace_spaces(category.name),replace_spaces(category.name), false, :name => "category[#{category.name}]", :onclick => ''$(''frm_categories'').onsubmit()'' %> <% end -%> I think that will work. -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Jennifer Maas
2008-Jan-17 20:40 UTC
Re: Returned RJS not executing when I submit a form with onclick instead of a submit tag
Thanks Justin. Using "onsubmit" instead of "submit" worked like a charm. I will try and change the code so that each checkbox has its own ajax call. I''m just a little concerned about how to tell the controller that a particular checkbox is on or off. I''m sure that''s a better way to do it though. Thanks again! On Jan 17, 12:11 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 17, 2008 1:39 PM, Jennifer Maas <jenniferm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am using prototype 1.5 *I think*. I put version 1.5 in my public > > javascript folder, so I assume that''s the version it''s picking up. Is > > there an easy way to tell? > > Just look at the prototype.js file, it''s right at the top. The latest > version if 1.6.0, there are many changes from 1.5 to 1.6. Also there > is a newer version of Scriptaculous (1.8) to go along with the new > version of Prototype. > > > Here''s the code to create the checkboxes (clicking them returns > > unexecuted javascript): > > > <%= check_box_tag > > replace_spaces(category.name),replace_spaces(category.name), false, > > :name => "category[#{category.name}]", > > :onclick => ''document.frm_categories.submit();'' %> > > When you call submit like that, you are submitting the form through > the normal form POST means, not the Ajax way. > > Why not just have individual checkboxes that have their own Ajax > request and do away with the remote_form? > > <%= check_box_tag > replace_spaces(category.name),replace_spaces(category.name), false, > :name => "category[#{category.name}]", > :onclick => remote_function( > :url => { :action => ''add_filter'' }, :with => "''#{ > replace_spaces(category.name) }='' + this.value" %> > > I''m taking advantage of another Rails helper method, remote_function, > which translates to a new Ajax request. See this page for more info:http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper... > > If you want to leave your code relatively untouched, and still use the > remote_form combined with each click of a checkbox causing an complete > form submit, try something like this: > > <% form_remote_tag :url => {:action => ''add_to_filter2''}, :html => > {:id => "frm_categories"} do -%> > <%= check_box_tag > replace_spaces(category.name),replace_spaces(category.name), false, > :name => "category[#{category.name}]", > :onclick => > ''$(''frm_categories'').onsubmit()'' %> > <% end -%> > > I think that will work. > > -justin--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Justin Perkins
2008-Jan-17 20:54 UTC
Re: Returned RJS not executing when I submit a form with onclick instead of a submit tag
On Jan 17, 2008 2:40 PM, Jennifer Maas <jennifermaas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m just a little concerned about how to tell the controller > that a particular checkbox is on or off.The code I sent previously can do this, with one slight modification. See below. <%= check_box_tag replace_spaces(category.name),replace_spaces(category.name), false, :name => "category[#{category.name}]", :onclick => remote_function( :url => { :action => ''add_filter'' }, :with => "''#{ replace_spaces(category.name) }='' + this.value + ''&checked='' + this.checked" %> Have a great day. -justin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---