AbstractController :: ActionNotFound错误,即使配置了routes.rb也是如此

时间:2013-08-26 22:53:16

标签: ruby-on-rails-3.2

我有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)

我需要改变什么?

1 个答案:

答案 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