有条件地将skip_before_filter应用于:if =>轨道条件4 [英] conditionally apply skip_before_filter with :if => condition in rails 4

查看:161
本文介绍了有条件地将skip_before_filter应用于:if =>轨道条件4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件我要跳过身份验证的控制器,事件是公开的。

在我的 ApplicationController 我有这个电话给devise的 authenticate_user!

  class ApplicationController< ActionController :: Base 
before_action:authenticate_user!
end

现在,在我的活动表中,我有一个名为公共。我用它来检查事件是否公开。像这样在 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 !,除非:is_public?

def is_public?
@ event.present? &安培;&安培; @ event.is_public
end
end

这可以作为期望和跳过身份验证如果 @ event.public = true ,因为上面在跳过后重复了 before_filter 与反向条件。



我想知道:


  1. 我做的是正确的?

  2. 这是否具有任何性能影响。那么有没有更好的方法?


解决方案

,之后,围绕行动)实际上是很糟糕的。看到这个类似的问题: skip_before_filter忽略条件



所以我总是参考导轨指南。对您来说有趣的部分在这里: http:// guides.rubyonrails.org/action_controller_overview.html#other-ways-to-use-filters



我不完全确定这将适用于跳过过滤器但是值得一试。



只要调用不同的过滤器就不会有性能影响。性能问题通常来自于广泛的数据库查询或其他外部系统调用。



我的主要关注点是,很难理解为什么会有这么多的before_action事情继续...


I have an Events Controller on which I want to skip authentication incase the event is public.

In my ApplicationController I have this call to devise's authenticate_user!

class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end

now, Inside my Events table, I have a boolean field called public. I use that to check if event is public or not. Like this in EventsController

class EventsController < ApplicationController
  skip_before_action :authenticate_user!, only: :show, if: Proc.new { :is_public? }
end

But for some reason, this didn't work. so I had to do this:

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

This works as expects and skip authentication if @event.public = true because the above repeats the before_filter with the inverse condition after skipping.

I am wondering:

  1. what I did is correct?
  2. Does this have any performance impact. if yes, then Is there a better way?

解决方案

the rails documentation on callbacks (before, after, around action) is actually pretty bad. see this similar question: skip_before_filter ignores conditionals

so i always refer to the rails guides. the part that would be interesting for you is here: http://guides.rubyonrails.org/action_controller_overview.html#other-ways-to-use-filters

i am not totally sure that this would work with skip filter as well, but it is worth a try.

there should not be a performance impact just by calling different filters. the performance issues usually come by extensive database queries or other external system calls.

my main concern here would be that it is pretty hard to understand why there are so many before_action things going on...

这篇关于有条件地将skip_before_filter应用于:if =&gt;轨道条件4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆