ErwinM
ErwinM

Reputation: 5131

When do before_filters in application_controller get called? Strange issue

I have developed a plugin. The plugin has a declaration that can be added to the application controller like so:

class ApplicationController < ActionController::Base

  set_something_to(Account.first)
end

That all works as expected. However, when I use a before_filter to dynamically fetch a value to set, like so:

class ApplicationController < ActionController::Base

  before_filter :get_account
  set_something_to(@account)

  protected
    def get_account
      @account = Account.first
    end
end

This does not work. The value that gets passed to the set_something_to declaration is nil. Why is the value nil? What would be the correct way of dynamically passing in a value?

Thanks for your time. Erwin

Upvotes: 0

Views: 217

Answers (1)

Kevin Sylvestre
Kevin Sylvestre

Reputation: 38012

Before filters are called before each action. If you want your set_something_to(@account) to work correctly, you should put it in a before filter as well. For example:

class ApplicationController < ActionController::Base

  before_filter :configure_account

  protected

    def configure_account
      @account = Account.first
      set_something_to(@account)
    end

end

Upvotes: 4

Related Questions