Rails의 특정 액션에 대한 인증 토큰을 어떻게 무시합니까?


168

진위 토큰을 확인하고 싶지 않은 특정 작업이있을 때 Rails가이를 확인하지 않도록하려면 어떻게해야합니까?

답변:


230

레일즈 4에서 :

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

그리고 레일 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

특정 컨트롤러 및 특정 작업의 경우 skip_before_filter : verify_authenticity_token, : only => : my_unprotected_action을 사용하십시오. 에 대한 답을 찾기 위해 여기에 왔습니다. 이것은 끔찍한 생각입니까? 아약스 응답이 내 세션을 먹기 때문에이 작업을 수행하려고합니다.
Danny

9
레일 5.2의 경우을 사용하십시오 skip_forgery_protection. API 문서를 참조하십시오 .
Aaron Breckenridge

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
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.