我有一个Events
控制器,我想在该事件公开时跳过身份验证。有条件地将skip_before_filter应用于:if => condition in rails 4
在我ApplicationController
我有这样的呼吁制定的authenticate_user!
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
现在,在我的活动表,我有一个布尔字段名为public
。我用它来检查事件是否公开或不公开。像EventsController
class EventsController < ApplicationController
skip_before_action :authenticate_user!, only: :show, if: Proc.new { :is_public? }
end
但是由于某种原因,这没有奏效。所以我不得不这样做:
class EventsController < ApplicationController
skip_before_action :authenticate_user!, only: :show
before_action :authenticate_user!, unless: :is_public?
def is_public?
@event.present? && @event.is_public
end
end
这个工作过程预计,跳过如果@event.public = true
因为上面跳过后重复before_filter
与逆条件认证。
我很纳闷:
- 我所做的是正确的?
- 这是否有任何性能影响。如果是,那么有更好的方法吗?
在代码的第二块,你忘了有目的的问号? 'Proc.new {:is_public}' – Damien
没有。我实际上无法得到这个':if => conditonal'来工作,所以不知道它应该是':is_pulic?'还是':is_public' – CuriousMind
如果你有一个'public'字段,它可以是'公共“还是”公共?“。如果你想使用它,请将'is_public?'方法移到你的'Event'模型中。 – Damien