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_codeand#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 に答える