Rails ajax调用404找不到路由错误

时间:2014-08-11 18:31:21

标签: jquery ruby-on-rails ruby ajax

我正在铁轨上玩ruby并尝试进行ajax调用,但一直看到错误。 所以在我的index.html.erb中,我有一个带id测试的按钮。在同一个索引文件中,我有

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

所以,当我点击带有id测试的按钮时,我会一直看到

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

我似乎无法弄明白为什么,因为在我的config / routes.rb中,我得到' / test',以:'提示#test'在我的promptsController中,我有

def test
  puts "enters here"
end

1 个答案:

答案 0 :(得分:4)

在你的路线中你说你有

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

但请注意,在致电$.ajax()时,您正在使用 POST

相反,您需要

的路线
post '/test', to: 'prompts#test'