Bill Walton
2006-Jul-26 18:00 UTC
[Rails] syntax for specifying :html fallback on form_remote_tag
Has anyone gotten the :html option for specifying a ''fallback'' action if javascript''s not enabled working on form_remote_tag (or remote_form_for)? I can''t figure out how to specify the action that''s supposed to be invoked if js _is_ enabled. Or maybe I''m just misunderstanding how this thing is supposed to work. I was assuming there''s some Rails magic going on where I can specify two actions, one to use if js is enabled, and a fallback to use if it''s not. Is that right? Thanks, Bill -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060726/df8f7119/attachment.html
Alex Wayne
2006-Jul-26 18:50 UTC
[Rails] Re: syntax for specifying :html fallback on form_remote_tag
Bill Walton wrote:> Has anyone gotten the :html option for specifying a ''fallback'' action if > javascript''s not enabled working on form_remote_tag (or > remote_form_for)? I can''t figure out how to specify the action that''s > supposed to be invoked if js _is_ enabled. > > Or maybe I''m just misunderstanding how this thing is supposed to work. > I was assuming there''s some Rails magic going on where I can specify two > actions, one to use if js is enabled, and a fallback to use if it''s not. > Is that right? > > Thanks, > BillIt should go to the same location you specify in your :url option, except via a full page request, not ajax. THen you handle the result with respond_to def some_action ... respond_to |type| type.html {redirect_to :action => ''list''} type.js #renders the default rjs file. end Try turning off javascript and see what it does. -- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Jul-27 16:21 UTC
[Rails] Re: syntax for specifying :html fallback on form_remote_tag
Hi Alex, Alex Wayne wrote:> > It should go to the same location you specify in your :url option, > except via a full page request, not ajax. THen you handle the result > with respond_to > > def some_action > ... > respond_to |type| > type.html {redirect_to :action => ''list''} > type.js #renders the default rjs file. > end > > Try turning off javascript and see what it does.It''s wierd. In IE, whichever redirect is listed first in the respond_to is executed. Doesn''t matter what the security setting is (that''s the only place I''ve found in IE that looks like it would be turning js off and on). FF, on the other hand, executes the wants.js option every time, independent of whether Javascript is enabled in the Options -> Content tab or not. I''ve included the view and controller code below. Sure would appreciate it if anybody could tell me what I''m doing wrong. TIA! Bill ---- view file that sends to controller to test for js enabled --- <%= form_remote_tag :html => {:action => url_for(:controller => ''create'', :action => ''check_js'')}%> <p>Select the appropriate button</p> <fieldset> <%= radio_button_tag "new_or_update", "new", checked = "true", :id => ''new_or_update_new'' %>Create a new eMRec.<br/> <%= radio_button_tag "new_or_update", "update", :id => ''new_or_update_update'' %>Update an existing eMRec.<br/> </fieldset> <p> <%= submit_tag("Create eMRec", :class => ''submit-btn'', :disable_with => "Please Wait") %> </p> <%= end_form_tag %> ---- check_js in create_controller.rb ---- def check_js respond_to do |wants| wants.html {redirect_to :action => ''no_js''} wants.js {redirect_to :action => ''choose_start_point''} end end ---- no_js in create_controller just renders view no_js.rhtml def no_js end ---- choose_start_point is the real entry point into the app ---- def choose_start_point ...... end
Alex Wayne
2006-Jul-27 18:26 UTC
[Rails] Re: Re: syntax for specifying :html fallback on form_remote_
Bill Walton wrote:> <%= form_remote_tag :html => {:action => url_for(:controller => > ''create'', > :action => ''check_js'')}%>There is no ajax url in there. Ajax calls don''t use whatever is in the action="foo" portion of the form tag. You want this instead: <%= form_remote_tag :url => { :controller => ''create'', :action => ''check_js'' } This will send you ajax request to that url AND put it as the form action. You were getting the same result because you were not using ajax since the ajax didnt know where to go. -- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Jul-27 22:26 UTC
[Rails] Re: Re: syntax for specifying :html fallback on form_remote_
Hi Alex, Alex Wayne wrote:> Bill Walton wrote: >> <%= form_remote_tag :html => {:action => url_for(:controller => >> ''create'', >> :action => ''check_js'')}%> > > There is no ajax url in there. Ajax calls don''t use whatever is in the > action="foo" portion of the form tag. > > You want this instead: > > <%= form_remote_tag :url => { > :controller => ''create'', > :action => ''check_js'' > }That didn''t work either. What happened to the :html option the documentation talks about? There''s a piece here I must be missing. Thanks, Bill
Alex Wayne
2006-Jul-27 22:59 UTC
[Rails] Re: Re: Re: syntax for specifying :html fallback on form_rem
Bill Walton wrote:> Hi Alex, > > Alex Wayne wrote: > >> <%= form_remote_tag :url => { >> :controller => ''create'', >> :action => ''check_js'' >> } > > That didn''t work either. What happened to the :html option the > documentation talks about? There''s a piece here I must be missing. > > Thanks, > BillWhat html does the above code create? It should look like: <form action="/create/check_js" onsubmit="...bunch of ajax stufff..."> -- Posted via http://www.ruby-forum.com/.
Bill Walton
2006-Jul-28 02:28 UTC
[Rails] Re: Re: Re: syntax for specifying :html fallback on form_rem
Hi Alex, Alex Wayne wrote:> What html does the above code create? It should look like: > > <form action="/create/check_js" onsubmit="...bunch of ajax stufff...">The answer and some variations on the theme below. ------------------------------------------------------------->>> <%= form_remote_tag :url => {:controller => ''create'', >>> :action => ''check_js'' >>> }generates <form action="/create/check_js" method="post" onsubmit="new Ajax.Request(''/create/check_js'', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"> ------------------------------------------------------------ <%= form_remote_tag :url => {:controller => ''create'', :action => ''choose_start_point''}, :html => {:action => url_for(:controller => ''create'', :action => ''check_js'')} %> generates: <form action="/create/check_js" method="post" onsubmit="new Ajax.Request(''/create/choose_start_point'', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;"> and, ------------------------------------------------------------ <%= form_remote_tag :url => {:controller => ''create'', :action => ''check_js''}, :html => {:action => url_for(:controller => ''create'', :action => ''choose_start_point'')} %> generates: <form action="/create/choose_start_point" method="post" onsubmit="new Ajax.Request(''/create/check_js'', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">------------------------------------------------------------ So my question from looking at the above is... Is the form action ever invoked? The :url option clearly controls the target of the Ajax request. So what''s the order of operation? It looks to me like maybe the form is going to try to invoke some js via the onsubmit. If js is not enabled, I assume then the form action takes over. Watching the console in Firebug, I can see the POST happen, then it disappears from the console, and the ''no_js'' page then renders. Is that the way it''s supposed to work? If so, what sort of response is the form waiting for? My problem is that with this in the view... <%= form_remote_tag :url => {:controller => ''create'', :action => ''check_js''} %> With this in the controller... def check_js respond_to do |wants| wants.html {redirect_to :action => ''no_js''} wants.js {redirect_to :action => ''choose_start_point''} end end I''m getting the ''no_js.rhtml'' template rendered whether Javascript is enabled through the menu via Tools->Options->Content or not. And the same results obtain when I put this into the controller... <%= form_remote_tag :url => {:controller => ''create'', :action => ''check_js''}, :html => {:action => url_for(:controller => ''create'', :action => ''no_js'')} %> I can see the ''POST'' message in Firebug, then it disappears, then the ''no_js.rhtml'' page renders. Clearly I''m misunderstanding and/or misinterpreting something. Any help figuring out what it is would be much appreciated! Thanks, Bill
Alex Wayne
2006-Jul-28 16:49 UTC
[Rails] Re: Re: Re: Re: syntax for specifying :html fallback on form
This is how an ajax form works. with JS enabled: 1. user click the submit button 2. The form''s onsubmit event is triggered 3. The javascript, which is an ajax request is executed, retrieved processed, page is updated. 4. The javascript in onsubmit then aborts the actual submit of the form by ending with "return false;" 5. Form is never actually submitted via "action", that property is completely ignored. with no JS the onsubmit javascript is simply ignored and the browser sends a vanilla post request to the server. It sounds like your browser is doing the ajax request, but then the entire page is being redirected, and not just the ajax request. The POST is disappearing in Firebug because a new page is loading. The problem is probably in your redirect. A redirect_to will have a "text/html" header. When prototype sees this it just renders this to the page, it doesn''t execute it as javascript. When this response has a redirect in it, the whole page redirects. So, don''t use redirects with ajax calls. If you change your respond_to block to: def check_js respond_to do |wants| wants.html {redirect_to :action => ''no_js''} wants.js {render(:update) {|page| page.alert(''rendered from rjs'')} end end I bethcha you will get a javascript alert box when you submit. so instead of redirect_to, you can simply use the fall-through (wants.js with no associated actions) which should use "check.rjs", or you can do a "render :action => ''choose_start_point''". Hope that makes sense. -- Posted via http://www.ruby-forum.com/.