# Respond\_to not detecting JS turned on. form\_to\_remote probl

**URL:** <https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996>\
**Category:** Rails\
**Created:** [August 30, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996 "2006-08-30T20:33:57Z")\
**Posts on this page:** 9\
**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:** [August 30, 2006, 8:33pm UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/1 "2006-08-30T20:33:57Z")

</div>

I’m trying to use respond\_to to redirect visitors who have JS turned off  
to a different page. The problem I’m having is that respond\_to is  
sending _everybody_ there. It’s not recognizing that JS is turned on in  
the browser. The wanted\_html.rhtml file below is rendered whether I’ve  
got JS turned on or off. I’ve looked at the Accept headers being sent  
from the form\_remote\_tag button using Live HTTP Headers. The headers  
are exactly the same whether JS is turned on or off. That seems wierd.

Another wierd thing. When the form is submitted, in the Firebug  
console, I see a POST, then it disappears. I put the sleep() call in  
below to verify. The POST is there until the respond\_to is evaluated.  
Then the POST disappears from the console. Any ideas? I’ll post the  
code below. TIA.

Bill

----- Controller -----

class CreateController \< ApplicationController

def index  
end

def edit  
sleep(5)  
respond\_to do |wants|  
wants.html { redirect\_to :action =\> ‘wanted\_html’ }  
wants.js { render }  
end  
end

def wanted\_html  
end

end

----- Views -----

— index.rhtml —

Click the button to test the respond\_to method

\<%= form\_remote\_tag :url =\> {:action =\> ‘edit’} %\>

 \<%= submit\_tag 'Test', :disable\_with =\> "Please Wait" %\> 
\<%= end\_form\_tag %\>

— edit.rjs —  
page.alert “You made it with JS turned on!”

— wanted\_html.rhtml —  
respond\_to says you don’t have JS turned on.

---

<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:** [August 30, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/2 "2006-08-30T23:00:34Z")

</div>

Bill W. wrote:

> I’m trying to use respond\_to to redirect visitors who have JS turned off  
> to a different page. The problem I’m having is that respond\_to is  
> sending _everybody_ there.

I noticed a similar problem in an application I’m building. I tried to  
use respond\_to but it did not consistently work. However, testing for  
request.xhr? works correctly everytime for me.

def edit  
if request.xhr?  
render  
else  
redirect\_to :action =\> ‘wanted\_html’  
end  
end

You might read through this thread for more information:  
[http://lists.rubyonrails.org/pipermail/rails/2006-June/049284.html](http://lists.rubyonrails.org/pipermail/rails/2006-June/049284.html)

---

<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:** [August 31, 2006, 12:56am UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/3 "2006-08-31T00:56:31Z")

</div>

Hi Curtis,

Thanks for your response!

Curtis S.

> I noticed a similar problem in an application I’m building. I tried to  
> use respond\_to but it did not consistently work. However, testing for  
> request.xhr? works correctly everytime for me.

I tried the code you suggested and it didn’t work either. Then I  
eliminated  
the ‘if’ and just tried to see if RJS was working. I think there’s  
something wrong with my setup.

I changed the controller so all the actions are empty and just call  
their  
views. The edit.rjs file contains just one line:  
page.alert “you have JS turned on”

What gets displayed is, rather than the dialog I expected, I get an html  
page that displays (rather than executes) the JS that Rails sent back.

try {  
alert(“you have JS turned ON”);  
}  
catch (e) {  
alert(‘RJS error:\n\n’ + e.toString());  
alert(‘alert(“you have JS turned ON”);’);  
throw e }

Any idea what might be causing this?

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:** [August 31, 2006, 3:14am UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/4 "2006-08-31T03:14:42Z")

</div>

On Aug 30, 2006, at 3:44 PM, Bill W. wrote:

> > for  
> > page.alert “you have JS turned on”  
> > alert(‘alert(“you have JS turned ON”);’);  
> > throw e }
> 
> Any idea what might be causing this?
> 
> Thanks,  
> Bill

```
Did you remember to do <%= javascript_include_tag :defaults %> in

```

the layout?

-Ezra

---

<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:** [August 31, 2006, 4:30am UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/5 "2006-08-31T04:30:51Z")

</div>

Hi Ezra,

Ezra Z. wrote:

> Did you remember to do \<%= javascript\_include\_tag  
> :defaults %\> in the layout?

Yes. And this is a test (i.e., tiny) app. I’ve included all the code  
below. I"m also going to try tomorrow and see is there’s any chance  
this is  
a dev vs. prod issue. Any insight you could provide will be hugely  
appreciated. Just fyi, I’m developing / seeing the problem on WinXP,  
SP2.  
I don’t understand the behavior I’m seeing. I manually check to make  
sure  
JS is enabled. It is. I click the button on the index page, which  
should  
render edit.rjs and throw up a message dialog. I get the following JS  
rendered instead.

try {  
alert(“you have JS turned ON”);  
} catch (e) { alert(‘RJS error:\n\n’ + e.toString()); alert(‘alert(“you  
have JS turned ON”);’); throw e }This is startin’ to …

Any insight will be grounds for worship 😉

Thanks,  
Bill

----- controller -----  
class CreateController \< ApplicationController

def index  
end

def edit  
end

def wanted\_html  
end

## end

## ----- views -----

— application.rhtml —

 \<%= javascript\_include\_tag :defaults %\> \<%= yield %\> 

— index.rhtml —

Click the button to test the respond\_to method

\<%= form\_remote\_tag(:url =\> {:action =\> ‘edit’}) %\>

 \<%= submit\_tag 'Test', :disable\_with =\> "Please Wait" %\> 
\<%= end\_form\_tag %\>

— edit.rjs —  
page.alert “you have JS turned ON”

— wanted\_html.rhtml —  
respond\_to says you have JS turned OFF.

---

<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:** [August 31, 2006, 8:47am UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/6 "2006-08-31T08:47:05Z")

</div>

Bill W. wrote:

> I’m trying to use respond\_to to redirect visitors who have JS turned off  
> to a different page.

> def edit  
> sleep(5)  
> respond\_to do |wants|  
> wants.html { redirect\_to :action =\> ‘wanted\_html’ }  
> wants.js { render }  
> end  
> end

Long shot, but have you tried to change order of wants.html and  
wants.js?

def edit  
sleep(5)  
respond\_to do |wants|  
wants.js { render }  
wants.html { redirect\_to :action =\> ‘wanted\_html’ }  
end  
end

Cheers,  
Bragi

---

<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:** [August 31, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/7 "2006-08-31T14:19:46Z")

</div>

Hi Lukasz,

Åukasz Piestrzeniewicz wrote:

> Long shot, but have you tried to change order  
> of wants.html and wants.js?

Yep. Tried it. No joy. But thanks for the reply!

Best regards,  
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:** [August 31, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/8 "2006-08-31T14:38:55Z")

</div>

Bill,

The disable\_with may be causing your problem due to a bug:

[http://dev.rubyonrails.org/ticket/5899](http://dev.rubyonrails.org/ticket/5899)

Take out the disable\_with and see what happens.

Curtis

---

<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:** [August 31, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/respond-to-not-detecting-js-turned-on-form-to-remote-probl/66996/9 "2006-08-31T19:14:41Z")

</div>

Thanks to David Black and Curtis S. for identifying the problem: DO  
NOT use the :disable\_with option on the submit\_tag for form\_remote\_tag,  
at least not until [http://dev.rubyonrails.org/ticket/5899](http://dev.rubyonrails.org/ticket/5899) is closed.

The code below the original post is a complete working app that  
demonstrates the use of respond\_to for redirecting non-JS- and  
JS-enabled browsers to different pages. I’ve left the original posting  
to assist future Railers’ searches.

----- Original Message -----  
From: Bill W.  
To: removed\_email\_address@domain.invalid  
Sent: Wednesday, August 30, 2006 1:26 PM  
Subject: [Rails] respond\_to not detecting JS turned on. form\_to\_remote  
problem?

I’m trying to use respond\_to to redirect visitors who have JS turned  
off to a different page. The problem I’m having is that respond\_to is  
sending _everybody_ there. It’s not recognizing that JS is turned on in  
the browser. The wanted\_html.rhtml file below is rendered whether I’ve  
got JS turned on or off. I’ve looked at the Accept headers being sent  
from the form\_remote\_tag button using Live HTTP Headers. The headers  
are exactly the same whether JS is turned on or off. That seems wierd.

Another wierd thing. When the form is submitted, in the Firebug  
console, I see a POST, then it disappears. I put the sleep() call in  
below to verify. The POST is there until the respond\_to is evaluated.  
Then the POST disappears from the console. Any ideas? I’ll post the  
code below. TIA.  
---- Controller ----  
class CreateController \< ApplicationController  
def index  
end

def edit  
respond\_to do |wants|  
wants.html { redirect\_to :action =\> ‘js\_inactive’ }  
wants.js {  
render :update do |page|  
page.redirect\_to :action =\> ‘js\_active’  
end  
}  
end  
end

def js\_active  
end

def js\_inactive  
end  
end

---- index.rhtml -----

Click the button to test if JS is activated.

\<%= form\_remote\_tag(:url =\> {:action =\> ‘edit’}) %\>

 \<%= submit\_tag 'Test for JS' %\> 
\<%= end\_form\_tag %\>

---- js\_active.rhtml ----

 \<%=h( "Your system has JS activated" )%\>

---- js\_inactive.rhtml ----

 \<%=h( "Your system does NOT have JS activated" )%\>

---- application.rhtml ----

 \<%= javascript\_include\_tag :defaults %\> \<%= @content\_for\_layout %\>
