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

Bill W. 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,
Bill

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.

Hi Alex,

Alex W. wrote:

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’)}%>

Select the appropriate button

<%= radio_button_tag "new_or_update", "new", checked = "true", :id => 'new_or_update_new' %>Create a new eMRec.
<%= radio_button_tag "new_or_update", "update", :id => 'new_or_update_update' %>Update an existing eMRec.

<%= submit_tag("Create eMRec", :class => 'submit-btn', :disable_with => "Please Wait") %>

<%= 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

Bill W. 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.

Hi Alex,

Alex W. 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,
Bill

Bill W. wrote:

Hi Alex,

Alex W. 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,
Bill

What html does the above code create? It should look like:

Hi Alex,

Alex W. wrote:

What html does the above code create? It should look like:

The answer and some variations on the theme below.


<%= form_remote_tag :url => {:controller => ‘create’,
:action => ‘check_js’
}

generates

<%= form_remote_tag :url => {:controller => ‘create’, :action =>
‘choose_start_point’},
:html => {:action =>
url_for(:controller
=> ‘create’, :action => ‘check_js’)} %>

generates:

and, ------------------------------------------------------------

<%= form_remote_tag :url => {:controller => ‘create’, :action =>
‘check_js’},
:html => {:action =>
url_for(:controller
=> ‘create’, :action => ‘choose_start_point’)} %>

generates:

------------------------------------------------------------

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

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.