【发布时间】:2009-07-24 13:58:23
【问题描述】:
当我不想检查真实性令牌的特定操作时,如何让 Rails 跳过检查它?
【问题讨论】:
标签: ruby-on-rails
当我不想检查真实性令牌的特定操作时,如何让 Rails 跳过检查它?
【问题讨论】:
标签: ruby-on-rails
Rails 5.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]
See all options @ api.rubyonrails.org
Rails 3 及以下:
skip_before_filter :verify_authenticity_token
【讨论】:
skip_forgery_protection。见API docs。
在 Rails4 中,您使用 skip_before_action 和 except 或 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
【讨论】: