route.rb 包含:
...
resources :mystuff
...
application_controller 包含:
...
before_action :check_login, except: [:login, :auth]
...
在登錄頁面上,我有一個link_to mystuff_path從未發生過的。
問題是,mystuff無論登錄狀態如何,我都希望允許所有操作。
我需要如何修改before_action以允許這樣做?(使用 Ruby 2.3.1,Rails 5.2.6)
我試過了:
...
before_action :do_stuff
...
def do_stuff
check_login unless mystuff_path || login_path || auth_path
end
...
它允許 mystuff-actions 和正常登錄,但會導致ActionController::InvalidAuthenticityToken,我認為這是無關的(與這個)問題需要解決。另外我不知道還有什么可能會以這種方式被破壞,例如:驗證??
uj5u.com熱心網友回復:
使用skip_before_action跳過回呼。例如:
class ApplicationController
before_action :authenticate_user! # secure by default
end
class PostsController < ApplicationController
skip_before_action :authenticate_user! # opt out of authentication for this controller
end
它采用相同的only,except,if和unless選項before_action可用于更精細的控制:
class PostsController < ApplicationController
skip_before_action :authenticate_user!, only: [:show, :index]
end
class PostsController < ApplicationController
skip_before_action :authenticate_user!, unless: :some_condition?
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359524.html
標籤:红宝石轨道
