1

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

我需要改变什么?

4

1 回答 1

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
于 2013-08-29T20:27:15.560 回答