Sharing among views


#1

Say I’ve got 2 controllers: Picture and Album

Now in the views I want to set up some instance variables for the layout
to use. But the same variables are needed for all the views for a given
controller.

So, e.g., in my picture list.rhtml I have

<%
@subtitle = ‘pictures’
@navigation = {
‘pictures’ => “selected”,
‘album’ => “”
}
%>

And I want the same for show.rhtml in Picture, etc, etc
And it will be different for all the Album views.

Now this seems pretty standard - so where does this stuff usually go?
Note that it’s in the view, but it’s all pure ruby - no html.

Maybe I can include some other partial template in these templates - one
for all the Picture views and one for the all the album.

But, since it’s the same for all views in a controller, and is all ruby,
it would seem nicer to put it in the controllers themselves.

But will the layout have access to instance variables from the
controller?
And where in the controller should it go such that the layout can access
it for all actions?


#2

rhubarb wrote:

Say I’ve got 2 controllers: Picture and Album

Now in the views I want to set up some instance variables for the layout
to use. But the same variables are needed for all the views for a given
controller.

you can add a method in your controller which would set these variables
and then use before_filter so that this method is invoked for each
action.


Agnieszka