Forum: Ruby on Rails sharing among views

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
61396c69040bbc56dbad1c5c71ac27d2?d=identicon&s=25 rhubarb (Guest)
on 2006-03-31 20:43
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?
5b132632f47e8c7e2a83157cef4649c8?d=identicon&s=25 Agnieszka Figiel (agnessa)
on 2006-03-31 21:50
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
This topic is locked and can not be replied to.