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.

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.

Hi Curtis,

Thanks for your response!

Curtis Summers

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

Did you remember to do <%= javascript_include_tag :defaults %> in the layout?

-Ezra

Hi Ezra,

Ezra Zygmuntowicz 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 :wink:

Thanks, Bill

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

  def index   end

  def edit   end

  def wanted_html   end

end

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

Hi --

Hi David, Curtis;

dblack@wobblini.net wrote:

I duplicated your problem and was able to solve it by removing the:disable_with option from the call to submit_tag.

Curtis Summers wrote:

The disable_with may be causing your problem due to a bug:

Thank you both very much!!! That was it. It never would have occurred to me to try that. Even reading the bug ticket didn't spark an 'aha' since I'm not doing any form processing. At any rate, removing the "disable_with" option fixed the problem completely. I'll send another email to post the code for future readers.

Best regards to you both, Bill

Thanks to David Black and Curtis Summers 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 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.

From: Bill Walton

To: rubyonrails-talk@googlegroups.com

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