andy
andy

Reputation: 2399

jQuery returns 404 for valid rails route?

I have this in my rails route:

scope '/api' do
    # ...
    resources :callbacks, only: [:create]
end

It was working (returning a 500 error) but I fixed that and suddenly I get a 404 error when this is run this bit of coffeescript:

  $('.add-callback button').on 'click', (e) ->
    $form = $(@).parent()

    $data =
      notes: $form.find('[name="callback-notes"]').val()
      date : $form.find('[name="callback-date"]').val()
      time : $form.find('[name="callback-time"]').val()

    $.ajax
      type: "POST",
      url: "/api/callbacks",
      data: $data,
      success: (data) ->
        if data.success
          $form.fadeOut ->
            location.reload true

and it returns this:

POST http://localhost:3000/api/callbacks 404 (Not Found) 

But POST /api/callbacks is found in the list of valid routes when you get a 404 anywhere else:

callbacks_path   POST  /api/callbacks(.:format)  callbacks#create

Any ideas?

Upvotes: 0

Views: 1095

Answers (1)

andy
andy

Reputation: 2399

I am utterly stupid. I found the answer using this bit of code in the console:

$.ajax({
  type:'POST', 
  url:'/api/callbacks', 
  complete:function(xhr){ document.body.innerHTML=xhr.responseText; }
})

The route was valid but it sent a 404 because a record I was trying to access inside the controller wasn't found.

Upvotes: 1

Related Questions