# Syntax for specifying :html fallback on form\_remote\_tag

**URL:** <https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046>\
**Category:** Rails\
**Created:** [July 26, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046 "2006-07-26T20:35:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 26, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/1 "2006-07-26T20:35:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 26, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/2 "2006-07-26T20:43:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 27, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/3 "2006-07-27T18:24:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 27, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/4 "2006-07-27T20:26:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 28, 2006, 12:28am UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/5 "2006-07-28T00:28:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 28, 2006, 12:58am UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/6 "2006-07-28T00:58:24Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 28, 2006, 4:32am UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/7 "2006-07-28T04:32:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 28, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/syntax-for-specifying-html-fallback-on-form-remote-tag/100046/8 "2006-07-28T18:49:09Z")

</div>

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.
