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

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

89716 次浏览

Rails5.2 +

您可以使用下面列出的相同 skip_before_action方法,或者使用新方法 skip_forgery_protection,它是 skip_before_action :verify_authenticity_token的一个瘦包装器

skip_forgery_protection

Rails 4 + :

# entire controller
skip_before_action :verify_authenticity_token


# all actions except for :create, :update, :destroy
skip_before_action :verify_authenticity_token, except: [:create, :destroy]


# only specified actions - :create, :update, :destroy
skip_before_action :verify_authenticity_token, only: [:create, :destroy]

查看所有选项@ api.rubyonrails.org


3号及以下铁路:

skip_before_filter :verify_authenticity_token

铁轨4中使用 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