kchoi
kchoi

Reputation: 1215

Rails ajax call 404 not found routing error

I was playing around with ruby on rails and tried making ajax call but kept seeing an error. So in my index.html.erb, I have a button with id test. And in the same index file, I have

("#test").click(function(){
    $.ajax({ 
      type: 'POST', 
      url: '/test', 
      data: {key: 'value'}, 
      dataType: 'script',
    });
});

So, when I click on the button with id test, I keep seeing

POST http://localhost:3000/test 404 (Not Found)

I cannot seem to figure out why because in my config/routes.rb, I have get '/test', to: 'prompts#test' and in my promptsController, I have

def test
  puts "enters here"
end

Upvotes: 0

Views: 3614

Answers (1)

Tiago Farias
Tiago Farias

Reputation: 3407

In your routes you say you have

get '/test', to: 'prompts#test' 

But notice that in your call to $.ajax() you're using POST.

Instead you need a route for

post '/test', to: 'prompts#test' 

Upvotes: 4

Related Questions