Instance Variables in Layout

I am new to rails, so I apologize if I use the wrong terminology.

I have a Menuitem model that I would like to display in the layout. How to pass instance variable to layout?

I was looking for a layout assistant, but I could not find anything. I also looked at defining an instance variable in an application controller to access it in a layout, will this work? If so, what is the best way to do this?

Thanks!

+7
ruby-on-rails layout
source share
5 answers

Two things I would like to point out. First, you probably do not want to make this request every time you produce any page in your application. You definitely want to cache your MenuItems. Secondly, it would be useful to put a convenience method in the MenuItems class to cache this value. So, if I define a method

def MenuItem.all_for_menu @@all_for_menu ||= MenuItem.find(:all) #returns value if exists, or initializes it end 

I can call MenuItem.all_for_menu in my layout and get all menu items. When you add a new one or edit it, you will have to invalidate it.

Another caching method would be to put the data in a partial and cache fragment using a standard caching call:

 <% cache(:controller => "menu_items", :action => "list", :action_suffix => "all_menu_items") do %> <%= render :partial => "menu", :collection => MenuItem.all_for_menu %> <% end %> 

You can then complete this snippet by calling:

 expire_fragment(:controller => "menu_items", :action => "list", :action_suffix => "all_menu_items") 
+3
source share

The usual way to pass variables from the view to the parent layout is to use the content_for method. (This answer is a copy + paste from a similar answer that I posted on this question )

The contents of a regular view are automatically converted to a yield call with no argument in the layout. But you can also place other placeholder content using yield with a character argument and specifying that content from the view using content_for .

app/views/layouts/posts_layout.html.erb

 <html> <head> <title>My awesome site</title> </head> <body> <div id="someMenuStructureHere"> <%= yield(:menu_items) %> <!-- display content passed from view for menu_items --> </div> <%= yield %> <!-- display main view content --> </body> </html> 

app/views/posts/index.html.erb

 <%= content_for :menu_items, some_helper_to_generate_menu %> <h1>Here is you page content</h1> 
+14
source share

Any instance variables defined in the controllers are automatically displayed in your views. If you expect an instance variable in your layout for all actions, you might want to define an instance variable in a before_filter file or encapsulate it in a controller method and use helper_method to make it available in your views.

0
source share

It really depends on what you want to do with model . I just guess, and you tell me what you need in order to better understand how to do this. This code will only work if your MenuItem model has a field named name .

In the controller:

 # Use whatever action you are currently displaying def index @menu_items = MenuItem.all end 

In the view file index.html.erb :

 <ul id="menu"> <% @menu_items.each do |menu_item| %> <%= h menu_item.name %> <% end %> </ul> 

Obviously, if it was a real menu, there would be hyperlinks :)

-one
source share

items_controller.rb (or something)

 def show @menu_item = MenuItem.find(params[:id]) end 

In the show.html.erb view:

 <%= @menu_item.name %> 
-2
source share

All Articles