conditionally apply skip_before_filter with :if => condition in rails 4

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

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?

回答1:

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...



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!