Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance variables in layout

I am fairly new to rails so I apologize if I am using the wrong terminology.

I have a model Menuitem that I would like to display the contents of in a layout. How does one go about passing an instance variable into a layout?

I was looking for a layout helper of some sort but I was unable to find anything. I was also looking at defining the instance variable in the application controller to access it in the layout, would this work? If so what is the best way to go about doing it?

Thanks!

like image 991
puttputt Avatar asked Dec 03 '09 03:12

puttputt


People also ask

What is an instance variable example?

Instance variables are specific to a particular instance of a class. For example, each time you create a new class object, it will have its copy of the instance variables. Instance variables are the variables that are declared inside the class but outside any method.

What is an instance of a variable?

An instance variable is a variable which is declared in a class but outside of constructors, methods, or blocks. Instance variables are created when an object is instantiated, and are accessible to all the constructors, methods, or blocks in the class.

What are instance variables in construct 3?

Instance Variables are added to object types but store numbers, text or booleans (on/off flags) individually for each instance. This makes them ideal for things like health counters in a game, since each instance tracks its own value.

What is instance variable in Obj C?

An instance variable is a variable that exists and holds its value for the life of the object. The memory used for instance variables is allocated when the object is first created (through alloc), and freed when the object is deallocated.


2 Answers

Any instance variables defined in the controllers are auto-magically available in your views. If you are expecting an instance variable in your layout for all actions, you may want to consider defining the instance variable in a before_filter or encapsulating it in a controller method and using helper_method to make it accessible in your views.

like image 79
Lytol Avatar answered Sep 23 '22 23:09

Lytol


The usual way of passing variables up from the view into the parent layout is to use the content_for method. (This answer is a copy + paste from a similar answer I posted at this question)

The normal view content gets rendered automatically into the yield call without an argument in the layout. But you can also put other placeholder content in by using yield with a symbol argument, and specifying that content from the view with 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>
like image 36
madlep Avatar answered Sep 23 '22 23:09

madlep