Loading JSON From Rails with jQuery, Avoiding 406 Error

First of all I should point out that I'm quite new to Rails (and jQuery) and the interactions between it and JQuery, which may be the reason that some of you may consider this a rather basic hint.
However whilst working on membermeister.com I've recently had some trouble loading JSON data from our Rails backend. It seemed like a very trivial task at first, all we needed was a GET request that was answered by a format.json response from the Rails controller.

Simplified the request looked something like this:

view plain
1$.ajax({
2    url: "/invoices/" + invoiceid + "/pay",
3    type: "GET"
4})

However Rails was returning a '406 Not Acceptable' error, suggesting the request I was sending was not something it was instructed to handle.

› Read Full Article


Ruby on Rails - Devise users/sign_out stops working

I'm currently working on a Ruby on Rails (RoR) app together with a friend of mine - I'm mainly handling the front end and hosting/config side of things whilst he is writing tons of Ruby code. I should add that we're using Twitter's bootstrap as a basis to style the application.

I would like to draw your attention to an issue that had me stumped for half a day today (and a few hours yesterday) and it manifested itself in the following error upon clicking a sign out link (we're using Devise for user auth):

view plain
1No route matches [GET] "/users/sign_out"

The obvious place to check was the routes, and indeed there was no GET route that matched. What should happen is that a DELETE request is fired once the user clicks the sign out link, which in turn is achieved by Rails' JavaScript code modifying the request behind the scenes to make it RESTful (DELETE instead of GET).
This topic of GET versus DELETE requests related to Devise sign outs is widely covered in posts such as this one or this one.

However this was not the issue we were seeing - it just looked very similar. It turned out that our version of Bootstrap which was a couple of months hold has a bug that stopped links inside nav dropdowns (if you use Bootstrap you know what I mean) with data-method attributes from working properly.

Once I realised that Rails' JS code inside jquery_ujs was no longer running when I clicked the link *inside* a nav dropdown but worked outside it I immediately googled for bootstrap related issues and found this bug report which is also related to this issue.

In short, upgrading bootstrap to version 2.2.1 fixed the issue for us and links would now be processed properly, sending a DELETE request when needed.

Hopefully this helps someone and saves them a few hours of grief - it was not the easiest bug to track down and fix.