Understanding before and after_filter

Jason Yost picture Jason Yost · Apr 10, 2011 · Viewed 10.5k times · Source

I have the following code right after my controller class declaration

  before_filter :load_form, :except => [:create, :update, :delete]
  after_filter :load_form, :only => [:create, :update, :delete]

  def load_form
    @severities = Severity.all
    @severity = Severity.new
  end

I get the list of severities when the page loads just fine, meaning that the before_filter is working. However after calling a method such as create the list of severities always comes back as nil. I think I am probably just missing some fundamental thing about filters. Can anyone help me understand why the after_filter does not work?

Answer

Steve Jorgensen picture Steve Jorgensen · Apr 10, 2011

If you were expecting to see variables in your view that were assigned in your 'after' filter, that won't work because the filter is not executed until after your view has been completely processed.