# Instance variable looses value upon render

**URL:** <https://discuss.rubyonrails.org/t/instance-variable-looses-value-upon-render/69705>\
**Category:** rubyonrails-talk\
**Created:** [October 8, 2014, 3:06pm UTC](https://discuss.rubyonrails.org/t/instance-variable-looses-value-upon-render/69705 "2014-10-08T15:06:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 8, 2014, 3:06pm UTC](https://discuss.rubyonrails.org/t/instance-variable-looses-value-upon-render/69705/1 "2014-10-08T15:06:35Z")

</div>

I have an instance variable, @pages, which needs to be set up in a certain way at the end of several actions, so I thought to be clever and do it in the following way:

# In my controller

after\_action :prepare\_admin\_home\_data, only: [:adm\_login,:adm\_upload\_selected]

def adm\_login &nbsp;&nbsp;... &nbsp;&nbsp;render admin\_pages\_home\_path &nbsp;&nbsp;... end

def prepare\_admin\_home\_data &nbsp;&nbsp;@pages=Dir["#{AMP\_DIR}/\*"] &nbsp;&nbsp;logger.debug("+++++++++++ pages #{@pages.to\_s}") end

I can see from the logs, that @pages got the right value, but in my view, it is nil. It seems that the HTML code is constructed before the after\_action is executed.

I understood the render() function in that way, that it only sets up which ERB template is supposed to be used, but actual rendering would occur only when the action has finished. Did I get this wrong?

Ronald

---

<div class="post-metadata">

**Author:** ![Matt\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@Matt\_Jones](https://discuss.rubyonrails.org/u/Matt_Jones)\
**Post date:** [October 9, 2014, 10:58am UTC](https://discuss.rubyonrails.org/t/instance-variable-looses-value-upon-render/69705/2 "2014-10-09T10:58:35Z")

</div>

> I have an instance variable, @pages, which needs to be set up in a
> 
> certain way at the end of several actions, so I thought to be clever and
> 
> do it in the following way:
> 
> # In my controller
> 
> after\_action :prepare\_admin\_home\_data, only:
> 
> [:adm\_login,:adm\_upload\_selected]
> 
> def adm\_login
> 
> …
> 
> render admin\_pages\_home\_path
> 
> …
> 
> end
> 
> def prepare\_admin\_home\_data
> 
> @pages=Dir[“#{AMP\_DIR}/\*”]
> 
> logger.debug(“+++++++++++ pages #{@pages.to\_s}”)
> 
> end
> 
> I can see from the logs, that @pages got the right value, but in my
> 
> view, it is nil. It seems that the HTML code is constructed before the
> 
> after\_action is executed.

Correct. Code in `after_action` callbacks can examine and mutate the about-to-be-sent response, but the render has already occurred.

> I understood the render() function in that way, that it only sets up
> 
> which ERB template is supposed to be used, but actual rendering would
> 
> occur only when the action has finished. Did I get this wrong?

When you call `render` the render happens immediately. If you return from an action _without_ rendering or redirecting, the default behavior will be executed by ActionPack (rendering the template with the same name as the action).

–Matt Jones

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [October 10, 2014, 8:19pm UTC](https://discuss.rubyonrails.org/t/instance-variable-looses-value-upon-render/69705/3 "2014-10-10T20:19:48Z")

</div>

Oh, I really got this wrong. So, the HTML is already constructed with the render call, then the rest of the action is executed!

Thanks a lot.

Ronald
