Instance variable looses value upon render

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.

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

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

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