Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 483145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:10:03+00:00 2026-05-13T01:10:03+00:00

I am fairly new to rails so I apologize if I am using the

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:10:03+00:00Added an answer on May 13, 2026 at 1:10 am

    Two things I would note. First, you probably don’t want to be doing this query every time you render any page in your application. You definitely want to cache your MenuItems. Second, it might be helpful to put a convenience method on 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 the menu items. When ever you add a new one or edit one, you’d have to invalidate that.

    Another caching approach would be to put the data in a partial and cache that fragment using the 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 expire that fragment by calling:

    expire_fragment(:controller => "menu_items", :action => "list", :action_suffix => "all_menu_items")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a fairly new Rails developer, and I have an existing application that I'm
I am fairly new to Rails and I have never developed a large application.
I'm fairly new in the Ruby + Rails scene. Although I have a very
I am fairly new to Ruby on Rails, and I clearly have an active
Fairly new to rails so forgive me if I've misunderstood anything. I have a
I'm fairly new to Rails, and I have a few beginner's questions. I want
I have a rails 3 application and I am fairly new to rails. I
I'm fairly new to Ruby on Rails, and I have a project with a
I'm fairly new to rails, and I'm developing a small application that involves user
I'm fairly new to Rails and am writing a login form. I have used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.