Ruby-on-rails 如何忽略 Rails 中特定操作的真实性令牌?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1177863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:31:36  来源:igfitidea点击:

How do I ignore the authenticity token for specific actions in Rails?

ruby-on-rails

提问by edebill

When I have a specific action that I don't want to check the authenticity token on, how do I tell Rails to skip checking it?

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

回答by edebill

In Rails 4:

在 Rails 4 中:

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

And Rails 3:

和 Rails 3:

skip_before_filter :verify_authenticity_token


For previous versions:

对于以前的版本:

For individual actions, you can do:

对于单个操作,您可以执行以下操作:

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

For an entire controller, you can do:

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

skip_before_action :verify_authenticity_token

回答by Epigene

In Rails4you use skip_before_actionwith exceptor only.

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