Is “proc” required with conditional before_action/before_filter?

后端 未结 4 1083
你的背包
你的背包 2021-02-05 00:05

Behold, a before_filter:

class ThingController < ApplicationController
  before_filter :check_stuff, :if => proc {Rails.env.production?}
end
<         


        
4条回答
  •  太阳男子
    2021-02-05 00:56

    i have done this on my code while ago. I hope that example helps to you. If you can use if statement but that should point to another method like I did here.

    class Admin::ArticlesController < ApplicationController
      before_filter :deny_access, :unless => :draft_and_admin?
    
      def show
        @article = Article.find(params[:id])
      end
    
      protected
    
      def draft_and_admin?
        Article.find(params[:id]).draft? && current_user.admin?
      end
    end
    

提交回复
热议问题