我有UserController
这些方法:
class UserController < ApplicationController
# snip
def test_add_realtime_code
end
def add_realtime_code
if request.post?
# snip
end
end
end
在我的routes.rb
,我有这个:
match '/user/add_realtime_code', :controller => 'user', :action => 'add_realtime_code', :via => :post
在/user/test_add_realtime_code.html.erb
中,我有一个按钮,可以将 AJAX POST 请求发送到/user/add_realtime_code
:
<div>
<script>
$(document).ready(function() {
$('#test_button').click(function() {
$.ajax({
type: 'POST',
url: '/user/add_realtime_code',
dataType: 'text',
data: { /* snip */ },
success: function(data, textStatus, jqXHR) {
$('#result').html(textStatus + ': ' + data);
}
});
});
});
</script>
<input type="button" name="test_button" id="test_button" value="test"></input>
<div id="result"></div>
</div>
即使我在 中设置了路由routes.rb
,但每当我单击按钮并发送 AJAX 请求时,我都会收到此错误:
AbstractController::ActionNotFound (The action 'add_realtime_code' could not be found for UserController)
我需要改变什么?