【发布时间】:2013-10-13 13:22:38
【问题描述】:
我有一个 Events 控制器,如果事件是公开的,我想在其上跳过身份验证。
在我的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 } -
不。我实际上无法让这个
:if => conditonal工作,所以不确定它应该是:is_pulic?还是:is_public -
如果你有一个
public字段,它可以是public或public?。如果您想使用is_public?方法,请将其移至您的Event模型。
标签: ruby-on-rails devise ruby-on-rails-4