skip_before_filter 忽略条件 [英] skip_before_filter ignores conditionals
问题描述
仅当应用程序处于生产模式时,我才尝试使用 skip_before_filter.(我不希望我的开发实例公开,我希望应用程序自动检测它所在的实例类型并在它不在生产模式时显示登录屏幕).所以,我的应用程序控制器有以下几行:
I'm trying to use skip_before_filter only if the app is in production mode. (I don't want my development instances public, and I want the app to automatically detect what type of instance it is on and display a log-in screen when it is not in production mode). So, my application controller has the following line:
before_filter :authenticate_user!, :except => "sign_in" #redirects to log-in
显示页面的控制器有这一行:
And the controller for displaying pages has this line:
skip_before_filter :authenticate_user!, :only => :show, :if => :in_production
#public pages are public, but only when in production.
而 in_production 就是:
And in_production is simply:
def in_production
ENV['RAILS_ENV']=='production'
end
我意识到这里可能还有其他途径,但我很好奇为什么skip_before_filter 似乎忽略了条件并且总是跳过before_filter.有什么我遗漏的吗?
I realize that there may be other avenues here, but I'm curious as to why skip_before_filter seems to ignore the conditional and always just skip the before_filter. Is there something I'm missing?
推荐答案
这是一个 Rails 错误(或者至少是一个未记录的奇怪行为).在这里跟踪:https://github.com/rails/rails/issues/9703
It is a Rails bug (or at least an undocumented strange behaviour). It is tracked here: https://github.com/rails/rails/issues/9703
在此线程中,您可以找到一个(扭曲的)解决方案.
In this thread, you can find a (twisted) solution.
代替
skip_before_filter :authenticate_user!, :only => :show, :if => :in_production
写
skip_before_filter :authenticate_user!, :only => :show
before_filter :authenticate_user!, :only => :show, :unless => :in_production
它对我有用.
这篇关于skip_before_filter 忽略条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!