Cur*_*ind 8 ruby-on-rails devise ruby-on-rails-4
我有一个Events控制器,我希望跳过身份验证,因为事件是公开的.
在我,ApplicationController我有这个设计的呼吁authenticate_user!
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
Run Code Online (Sandbox Code Playgroud)
现在,在我的事件表中,我有一个名为的布尔字段public.我用它来检查事件是否公开.像这样EventsController
class EventsController < ApplicationController
skip_before_action :authenticate_user!, only: :show, if: Proc.new { :is_public? }
end
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,这没有用.所以我不得不这样做:
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
Run Code Online (Sandbox Code Playgroud)
这可以按预期工作并跳过身份验证,@event.public = true因为上面的内容before_filter在跳过后重复了反向条件.
我想知道:
回调中的rails文档(之前,之后,围绕操作)实际上非常糟糕.看到这个类似的问题:skip_before_filter忽略条件
所以我总是参考导轨指南.你感兴趣的部分是:http://guides.rubyonrails.org/action_controller_overview.html#other-ways-to-use-filters
我不完全确定这也适用于跳过滤镜,但值得一试.
通过调用不同的过滤器不应该对性能产生影响.性能问题通常来自广泛的数据库查询或其他外部系统调用.
我在这里主要担心的是,很难理解为什么会有这么多的before_action事情发生......
| 归档时间: |
|
| 查看次数: |
12110 次 |
| 最近记录: |