Topic: Instance variable set in after_filter is not available in view

Instance variable set in after_filter is not available in a view.

When I set a instance variable in a filter

class Admin::CarMakeController < Admin::BaseController
  after_filter :do_sth

...
  def do_sth
    @abcdef = "set in filter"
  end
end


I cannot access it in the view. It's nil/empty.
The filter is being run (tested it with 'render_text'). When I set this variable in action, it is available in views.

Manual says that :

Filters have access to the request, response, and all the instance variables set by other filters in the chain or by the action (in the case of after filters).

so they are able to set instance variables too.

Am I missing something

Thanks for help

------------

I'll answer myself -- you cant set @variable in after_filter hmm

Last edited by dr_bonzo (2006-11-24 16:59:28)

Re: Instance variable set in after_filter is not available in view

Unless I misunderstand the flow, the after_filter is executed after the call to render, so you are setting the instance variable after the view has been rendered to the erb stream. Perhaps I should ask a question: After what do you want this filter to execute?

Re: Instance variable set in after_filter is not available in view

It was part of bigger problem which I have solved with different method.

I needed to check if @some_object was assigned in action and then assign @another_object to be available in views.

With after_filter I cannot create controllers instace variables, but i can call render :text and it renders this text

Re: Instance variable set in after_filter is not available in view

response.body should be fully formed by the time you hit the after_filter. That's why you can use after_filter to remove whitespace or gzip the pages you serve. I'm not sure why render works for you in your after_filter, but sounds like you're on your way anyhow.