0

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?

2
  • You haven't included the obvious bit of information: What happens if you just type http://localhost:3000/api/callbacks into your browser address bar? In theory, I'd expect you to get an error because of using GET rather than POST, but if you get a 404 that would suggest an avenue of investigation... Or use wget or curl or similar to actually do a POST to it. It's extremely unlikely that jQuery is the issue here. Commented Dec 12, 2013 at 15:34
  • Returns a 404 due to it being a GET request. But I included the line in the 'valid routes' bit of the 404 page that states the above should be valid. I tried CuRL but I get Invalid authenticity token Commented Dec 12, 2013 at 15:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.