问题描述
我对 skip_before 操作有一些问题:
i've some problem with the skip_before action:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :require_login
before_action :inc_cookies
def inc_cookies
if cookies[:token] != nil
@name = cookies[:name]
@surname = cookies[:surname]
@user_roomate = cookies[:roomate]
end
end
def require_login
if cookies[:token] == nil
puts "No token"
redirect_to '/'
end
end
end
和我的另一个控制器:
class UsersController < ApplicationController
skip_before_action :require_login, :except => [:landing, :connect, :create]
end
我不知道为什么,但是当我在 root 上时(来自 UsersController 的 :landing 操作),Rails 尝试传入 require_login...我对这个过滤器有什么误解,还是我有什么问题?
I don't know why, but when I'm on the root (the :landing action from UsersController), Rails try to pass in the require_login...I've misundertood something with this filter, or do I something wrong?
感谢您的帮助!
推荐答案
这对我来说听起来很正常 - 你已经要求 rails 跳过你的 before 动作,除非动作是 :landing
, :connect
或 :create
而听起来好像你想要相反的.如果您希望这 3 个操作不执行 require_login
那么您应该这样做
This sounds normal to me - you've asked rails to skip your before action, except if the action is :landing
, :connect
or :create
whereas it sounds as though you want the opposite. If you want those 3 actions not to execute the require_login
then you should be doing
skip_before_action :require_login, :only => [:landing, :connect, :create]
这篇关于Rails 在操作不起作用之前跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!