我有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
中,我有一个向/user/add_realtime_code
发送AJAX POST请求的按钮:
<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)
我需要改变什么?
答案 0 :(得分:3)
我意外地制作了UserController#test_add_realtime_code
和#add_realtime_code
方法private
:
class UserController < ApplicationController
# snip
private
# snip
def test_add_realtime_code
end
def add_realtime_code
if request.post?
# snip
end
end
end
当我将它们移出控制器的private
方法时,一切都按预期工作:
class UserController < ApplicationController
# snip
def test_add_realtime_code
end
def add_realtime_code
if request.post?
# snip
end
end
private
# snip
end