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

ede*_*ill 165 ruby-on-rails

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

ede*_*ill 223

在Rails 4中:

skip_before_action :verify_authenticity_token, except: [:create, :update, :destroy]
Run Code Online (Sandbox Code Playgroud)

和Rails 3:

skip_before_filter :verify_authenticity_token
Run Code Online (Sandbox Code Playgroud)

对于以前的版本:

对于个人行为,您可以:

protect_from_forgery :only => [:update, :destroy, :create]
#or
protect_from_forgery :except => [:update, :destroy, :create]
Run Code Online (Sandbox Code Playgroud)

对于整个控制器,您可以:

skip_before_action :verify_authenticity_token
Run Code Online (Sandbox Code Playgroud)

  • 对于rails 5.2,请使用`skip_forgery_protection`.参见[API docs](http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html#method-i-skip_forgery_protection). (6认同)

Epi*_*ene 27

Rails4您使用skip_before_actionexceptonly.

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
Run Code Online (Sandbox Code Playgroud)