如何忽略Rails中特定操作的真实性标记?

时间:2009-07-24 13:58:24

标签: ruby-on-rails

当我有一个特定的操作,我不想检查真实性标记时,如何告诉Rails跳过检查?

2 个答案:

答案 0 :(得分:218)

在Rails 4中:

skip_before_action :verify_authenticity_token, except: [:create, :update, :destroy]

和Rails 3:

skip_before_filter :verify_authenticity_token

对于以前的版本:

对于个人行为,您可以:

protect_from_forgery :only => [:update, :destroy, :create]
#or
protect_from_forgery :except => [:update, :destroy, :create]

对于整个控制器,您可以执行以下操作:

skip_before_action :verify_authenticity_token

答案 1 :(得分:26)

Rails4 中,您skip_before_actionexcept使用only

class UsersController < ApplicationController
  skip_before_action :verify_authenticity_token, only: [:create]
  skip_before_action :some_custom_action, except: [:new]

  def new
    # code
  end

  def create
    # code
  end

  protected
  def some_custom_action
    # code
  end
end