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!

Jeff Escalante
  • 3,137
  • 1
  • 21
  • 30

1 Answers1

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)

Alex Marchant
  • 2,490
  • 27
  • 49
  • 1
    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 ~ – Jeff Escalante Nov 21 '11 at 21:34
  • 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. – Jeff Escalante Nov 21 '11 at 22:06
  • 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. – Alex Marchant Nov 21 '11 at 22:15
  • 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. – Alex Marchant Nov 21 '11 at 22:34
  • 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. – Jeff Escalante Nov 22 '11 at 04:33
  • 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) %>'`) – seymore_strongboy Oct 03 '18 at 13:36