43

Behold, a before_filter:

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

During a recent code review, I was asked, "Is the proc is required for this to work?" The answer appears to be 'yes', but it's a reasonable question, and I had intended to answer it by referring to the Rails docs or guides or something on the use of conditionals with before_filter (now an alias of before_action).

I couldn't find any. The Action Controller Guide mentions :only/:except, but not :if/:unless.

Failing that, is there somewhere in the code I can point to that covers this? It's mentioned briefly here, but that's more about how :only and :except are handled, rather than :if or :unless.

5 Answers 5

63

Found it on Rails Guides: http://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks

Turns out a Proc isn't always required for it to work.

the :if and :unless options, which can take a symbol, a string, a Proc or an Array.

So in your case you could probably get away with

before_action :check_stuff, if: "Rails.env.production?"

Finding things in Rails documentation can be a pain sometimes, but at least questions like this make things easier to find over time since StackOverflow is well indexed and has high search rankings.

2
  • 2
    Technically, those are ActiveRecord callbacks, rather than ActionController ones, but I imagine the option syntax is the same. I'm not sure if I like eval'd strings better than Proc, but thanks for finding the docs. Nov 9, 2014 at 6:19
  • 4
    Looks like ActionController and ActiveRecord use the same ActiveSupport::Callbacks implementation: api.rubyonrails.org/classes/ActiveSupport/Callbacks.html ActionController does some normalization to convert :only and :except options to the :if and :unless options ActiveSupport::Callbacks expects.
    – adamesque
    Dec 13, 2014 at 18:17
28

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
12

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
3
  • 1
    That's useful to know, but doesn't address my actual question, which is about documentation. Apr 29, 2014 at 15:27
  • There will not be documentation about if and unless because people are using condition inside the method
    – jimagic
    Apr 29, 2014 at 15:30
  • This is fantastic Oct 10, 2018 at 22:11
6

I would recommend using staby lambda. If you want know, WHY? Please read this

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

which is almost equivalent to Upvote Me's answer.

0

Adding a method to check the if/unless conditions for a before_action should be the best way as this way you can easily accommodate any additional changes in the before_action conditions easily in future:

class ThingController < ApplicationController
  before_filter :check_stuff, if: :check_stuff?

  def check_stuff
  end

  private

  def check_stuff?
    Rails.env.production?
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.