Quantcast
Channel: davidfurber.name » Ruby on Rails
Viewing all articles
Browse latest Browse all 6

Instance variable not assigned in view

$
0
0

I just spent a few minutes in la-la-land trying to figure out when an instance variable assigned in the controller stopped showing up in the view, despite all evidence that it was getting set correctly in the controller.

The answer was that there were several conditions that would case an update action to render :action => :edit. I was being clever by moving them to the top of the function, however then I was assigning that instance variable AFTER I had called render.

Lesson learned: Although controller code keeps on executing after you call render (unless you follow it with an explicit return), no more instance variables will get passed on to the view. It is obvious now why: a Rails request executes a given action in a controller. The controller executes before filters, executes the action, yields to whatever template has been defined (or searches for the automatic one if render has not been called). The view then yields back to the controller, which executes its after filters then back up through the middleware and out the door. (Ever notice if you try to yield between methods in a controller it wets the bed?) If you render before you’re done doing what you need to do, the controller has initialized the view template but keeps on executing controller code before yielding to it.


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images