仅当应用程序处于生产模式时,我才尝试使用 skip_before_filter。 (我不希望我的开发实例公开,我希望应用程序自动检测它所在的实例类型并在它不在生产模式时显示登录屏幕)。所以,我的应用程序 Controller 有以下几行:
before_filter :authenticate_user!, :except => "sign_in" #redirects to log-in
用于显示页面的 Controller 有这一行:
skip_before_filter :authenticate_user!, :only => :show, :if => :in_production
#public pages are public, but only when in production.
而 in_production 很简单:
def in_production
ENV['RAILS_ENV']=='production'
end
我意识到这里可能还有其他途径,但我很好奇为什么 skip_before_filter 似乎忽略了条件并总是跳过 before_filter。有什么我想念的吗?
最佳答案
这是一个 Rails 错误(或者至少是一个未记录的奇怪行为)。它在此处跟踪:https://github.com/rails/rails/issues/9703
在此线程中,您可以找到一个(扭曲的)解决方案。
代替
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
它对我有用。
关于ruby-on-rails - skip_before_filter 忽略条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637110/