Skip to content

Instantly share code, notes, and snippets.

@ordinaryzelig
Created March 28, 2013 20:23
Show Gist options
  • Save ordinaryzelig/5266470 to your computer and use it in GitHub Desktop.
Save ordinaryzelig/5266470 to your computer and use it in GitHub Desktop.

If you're using AJAX in Rails 3 with jQuery, there's a little gotcha you may want to know about, especially if your'e at the laundromat.

I was working at the laundromat and getting this really weird error. Every remote link was broken. Whenever I clicked on a link that was supposed to do some AJAX magic, I would get a routing error that the url wasn't being matched. Looking at the log, it was requesting with GET instead of PUT, POST, etc. I looked at the HTML source and everything looked fine:

data-remote="true" "data-method="put"

I looked through my changes and saw nothing that would have affected it. All my integration tests passed, so it couldn't have been any broken Ruby code. (By the way, can anybody please recommend a good way to test AJAX in Rails 3 and Ruby 1.9.2?) Maybe it's a problem with the JavaScript. Time to do some good ole alert() JavaScript debugging:

// application.js
$(document).ready(function() {
  alert()
})

Nada. I even removed all JavaScript code except this and the rails.js. And then it hit me. It's the laundromat's fault. I have no internet here. No internet means no access to google, the source of my jQuery library. I need a helper:

# application_helper.rb
def jquery_include_tag
  if Rails.env == 'production'
    javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'
  else
    # don't forget to actually include the library in public/javascripts.
    javascript_include_tag 'jquery-1.4.2.min.js'
  end
end

Now, when I'm not connected to the internet, my jQuery will still work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment