We build Web & Mobile Applications.

< All Articles

Halting the filter chain in Rails 2.0

Here’s one that could have you scratching your head in puzzlement for a little while. In Rails 1.2.x you could do something like this in your controller:

class PostsController < ActionController::Base
  before_filter :do_filter

  # Controller code

  protected
    def do_filter
      # Filter logic goes here

      # Halt the filter chain
      false
    end
end

If the filter returned false then processing of the request would stop and you’d see a blank page in your browser.

Not so in Rails 2.0: thanks to changeset 7984 the filter chain is only halted if you render or redirect within your filter. If you really want to get similar behaviour to Rails 1.2.x you could always do this:

def do_filter
  # Filter logic goes here

  # Halt the filter chain
  head :bad_request
end

Although you will probably be more popular with your users if you display some kind of friendly error message!

Updated on 07 February 2019
First published by Rob Anderton on 12 December 2007
© Rob Anderton 2019
"Halting the filter chain in Rails 2.0" by Rob Anderton at TheWebFellas is licensed under a Creative Commons Attribution 4.0 International License.