Rails 在操作不起作用之前跳过 [英] Rails skip before action doesn't work

查看:43
本文介绍了Rails 在操作不起作用之前跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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 在操作不起作用之前跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆