Is “proc” required with conditional before_action/before_filter?

后端 未结 4 1093
你的背包
你的背包 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 01:00

    From Rails 5.2 onwards, the current accepted answer is no longer be valid, and passing a string to the conditional will fail.

    DEPRECATION WARNING: Passing string to :if and :unless conditional options is deprecated and will be removed in Rails 5.2 without replacement.

    Going forward, a proc is now the best way to add a conditional like in the original question:

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

提交回复
热议问题