ajax request to rails not working check here

First things first… When Chrome gives you a Bad Request CORS error, that’s not necessarily a true story. The true story lies within the Heroku logs. I was getting the Bad Request error and when I checked the heroku logs, I got an error that I didn’t recognize right away, but it was an error that I’d seen before.

How many times will I forget this error? I was getting a “No Template” Error when I was doing a API request from a wordpress website. This took me at least an hour to figure out. And it’s like the 4th fucking hour I’ve spent on this error because I keep forgetting it. The major problem is that EVERYTHING works with Postman, but when I put the request live on my website, it didn’t work. That was the start of the frustration.

406 NOT ACCEPTABLE

ILL TELL YOU WHAT ISNT ACCEPTABLE. THIS FUCKING ERROR ISNT ACCEPTABLE. I SPENT ABOUT 2 HOURS JUST NOW DEBUGGING THIS STUPID ASS BULLSHIT ERROR. THANK GOD THERE WERE PEOPLE WHO HELPED ME OUT.

Completed 406 Not Acceptable in 103ms

ActionController::UnknownFormat (ActionController::UnknownFormat):

From my understanding, this error occurs WHEN something is requested and you don’t have a respond_to that matches it. WELL THAT WAS FUCKED UP BECAUSE I WAS SENDING JSON AND I SET MY CONTROLLER UP TO ACCEPT JSON. But when I looked a little closer at them logs on heroku, I FOUND THE MOTHER FUCKING CULPRIT.

Processing by TextAlertsController#create as HTML

THAT BASTARD THOUGHT THAT I WAS SENDING IT HTML. Fucker.

So well, fuck. The ANSWER…

Some people said, hey go add contentType: "application/json", to your ajax request… DIDNT WORK

Some people said, hey go add dataType: "json", to your ajax request… DIDNT WORK

Some poeple said, hey go add

data: JSON.stringify({ 
  text_alerts: {
   phone_number: phone_number,
   text_frequency: text_frequency
  }
})

THAT DIDNT WORK. Btw, that’s how you nest attributes and get rails to understand them.

BUT WHAT DID WORK WAS THIS… Add default format as json in my routes.rb

resources :text_alerts, only: [:create, :update], defaults: {format: :json}

SO ALL I CAN SAY IS ONCE AGAIN FUCK YOU RAILS FUCK YOU CORS FUCK YOU GOOGLE CHROME. FUCK ALL YALL. (Use ALL YALL when you have 3 or more parites)

Here is the request that I started with…

var jqxhr = $.getJSON( "http://mysite.com/memberships", function() {
  console.log( "success" );
})

That’s when I got the missing template error. AND I KNOW THE FUCKING TEMPLATE IS THERE.

But the problem is with the URL. Add .json.

var jqxhr = $.getJSON( "http://mysite.com/memberships.json", function() {
  console.log( "success" );
})

Rails is expecting a .json request. You never have to use the .html when requesting a html template. I guess rails makes that the default request. Even if you tell your controller to respond_to :json, it still needs the .json in the url.

I can only say I have one saving grace, I don’t give up on a problem. I still need to recognize that I’m getting frustrated and take a break. I did that, and that was the best thing I did today.

Missing Template for Show

Let’s say I’m creating an AJAX request for a CREATE action. I accidentally made a redirect on the create controller to the show action. That caused an error to come up saying the show template didn’t exist. I thought it was a failure on the create action side, but if I looked up a few lines, I saw that it was actually creating the object.

How to render return the json ajax response in rails

First things first. Just add a json view to the create action.

Post Content