Ruby-on-rails Rails 在操作不起作用之前跳过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27204055/
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 23:59:50  来源:igfitidea点击:

Rails skip before action doesn't work

ruby-on-rails

提问by Julien Leray

i've some problem with the skip_before action:

我对 skip_before 操作有一些问题:

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

and my other controller:

和我的另一个控制器:

class UsersController < ApplicationController
 skip_before_action :require_login, :except => [:landing, :connect, :create]
end

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?

我不知道为什么,但是当我在 root 上时(来自 UsersController 的 :landing 操作),Rails 尝试传入 require_login ......我误解了这个过滤器的某些内容,还是我有什么问题?

Thanks for any help!

谢谢你的帮助!

回答by Frederick Cheung

This sounds normal to me - you've asked rails to skip your before action, except if the action is :landing, :connector :createwhereas it sounds as though you want the opposite. If you want those 3 actions not to execute the require_loginthen you should be doing

这对我来说听起来很正常 - 你已经要求 rails 跳过你的 before 动作,除非动作是:landing:connect或者:create听起来好像你想要相反的动作。如果您不希望这 3 个操作不执行,require_login那么您应该这样做

skip_before_action :require_login, :only => [:landing, :connect, :create]