Ruby on rails 使用:if=>;有条件地在过滤之前应用跳过;轨道4的状况
我有一个Ruby on rails 使用:if=>;有条件地在过滤之前应用跳过;轨道4的状况,ruby-on-rails,devise,ruby-on-rails-4,Ruby On Rails,Devise,Ruby On Rails 4,我有一个Events控制器,如果事件是公共的,我想跳过该控制器上的身份验证 在我的ApplicationController中,我调用了Desive的authenticate\u用户 class ApplicationController < ActionController::Base before_action :authenticate_user! end 但出于某种原因,这不起作用。所以我必须这样做: class EventsController < Applicati
Events
控制器,如果事件是公共的,我想跳过该控制器上的身份验证
在我的ApplicationController
中,我调用了Desive的authenticate\u用户代码>
class ApplicationController < ActionController::Base
before_action :authenticate_user!
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
,这将按预期工作并跳过身份验证,因为上述操作会在跳过后以相反的条件重复筛选前的
我想知道:
我所做的是正确的吗
这对性能有影响吗。如果是,那么还有更好的方法吗
关于回调的rails文档(操作之前、之后和之后)实际上相当糟糕。见类似问题:
所以我总是参考导轨。您感兴趣的部分是:
我不完全确定这是否也适用于skip filter,但值得一试
仅仅调用不同的过滤器不应该对性能产生影响。性能问题通常由大量的数据库查询或其他外部系统调用引起
我在这里主要担心的是,很难理解为什么会有这么多未采取行动的事情发生 在第二段代码中,您是否故意忘记了问号Proc.new{:is_public}
Nopes。实际上,我无法让这个:if=>conditional
工作,所以不确定它是否应该是:is_pulic?
或:is_public
如果你有public
字段,它可以是public
或public?
。如果要使用is\u public?
方法,请将其移动到事件
模型。
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