rails ujs - javascript not executing
Asked Answered
W

1

10

This is a very strange problem. I've got a rails app in which I am replacing one partial on the page with another using UJS. Recently, it stopped worked, so I started logging throughout the process to see where it broke. Here's what I found:

  • After the link is clicked, the rails console tells me that everything rendered, including all the partials, and returns 200 OK.
  • A puts in the controller actions returns fine
  • A puts in the respond.do block returns fine
  • A puts in the .js.erb file returns fine

This means it's getting all the way to running the .js.erb file and evaluating the ruby with no errors. But here's what's weird - none of the javascript is executing. When I run an alert('hello?') in the .js.erb file, it's not executing.

Has anyone ever run into this problem before? It seems exceedingly strange that it would run the erb evaluation but not the js. And no errors appear in the js console either.

For the record I have: cleared the cache, restarted the browser, restarted the rails app, and nothing changed.

Sample code:

# view
link_to 'example', some_path, :remote => true

# controller
respond_to do |format|
  format.js
end

# .js.erb file
alert('this never shows up');
<% puts 'this shows up in the rails console' %>

Thanks soo much to anyone who can shed a bit of light on this strange problem!

Wakayama answered 19/11, 2011 at 23:58 Comment(7)
"And no errors appear in the js console either."Wakayama
Sorry; I automatically ignore sentences starting with conjunctions.Alf
You say that it recently stopped working -- Any chance that you added some JS on your page that is interfering with the return?Buhler
If you are using chrome, you can also see what the browser received: does that look ok?Myrick
Yep, I'm seeing it come in in the console under XHR - the status is 200 OK and it's showing all the javascript from the .js.erb file as the response : / (here's a screenshot - cl.ly/BxmT - I recorded it coming in)Wakayama
What method are you using to replace the content? Which version of rails ujs are you using - prototype based or jQuery based?Paraldehyde
jquery based and I'm using the jquery method replaceWith()Wakayama
H
10

Any chance there is an error in the js? The ajax request doesn't show up normally in the js console. http://www.alfajango.com/blog/rails-js-erb-remote-response-not-executing/

(A similar environment runs fine on my machine)

Holmun answered 21/11, 2011 at 21:16 Comment(6)
Haha your right, it shows up in the network tab of the chrome inspector (under XHR, as I mentioned). The javascript console remains empty. This is a great post though, I'll check it over and see if that solves it! Thanks ~Wakayama
In this article it says to check if it's being processed "as JS" - for me, it says it is being processed as JS. So I clicked the alternate link you provided and it gave me a 404 error.Wakayama
Ya i realized that too :( but the bottom of the article provides a way to test the rendered js for errors in firebug. Which may provide you with some insight into whats going on.Holmun
Just read again the article says "If the “as JS” bit is in there, then what we have is a JavaScript problem (which is addressed below)." So you shouldn't need the page at the link anyways, just the article.Holmun
thanks so much for the guidance here - strangely enough, I opened up firefox to test it, and it worked. Then I restarted chrome, and made some new entries. It worked for all the new entires, just not these three particular ones I was testing before. How weird is that? Either way, I think this is more or less resolved, although I will see if I can figure out what the issue is with those three strange examples.Wakayama
In my experience, whenever this happens, it's always a Javascript error, often caused by inline Ruby. For example, I had something like this: url: <%= thing_path(@thing) %>, which looked fine, but was of course missing quotation marks around the path name (should have been url: '<%= thing_path(@thing) %>')Pettitoes

© 2022 - 2024 — McMap. All rights reserved.