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

  • Home
  • SEARCH
  • 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 6211719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:23:12+00:00 2026-05-24T06:23:12+00:00

I have a variable I want to access in my application.html.erb so it is

  • 0

I have a variable I want to access in my application.html.erb so it is available to all views. I know I can go in and add it to each controller and add it to each function so it is available to index, show, etc. However, that is crazy. If I want it to be in a header I should be able to access it in the application.html.erb file so it is in all my views. An example of one of my controllers it is working in…

     def index
    if session[:car_info_id]
      @current_car_name = current_car.name
    end

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @miles }
    end
  end

Here is my function in applicationcontroller:

  private   

  def current_car
    CarInfo.find(session[:car_info_id])
  end

If I try to add @current_car_name = current_car.name to my applicationcontroller so the variable is available to my application.html.erb it bombs with a Routing to current_car error. I know the function works because if I call it in a different controller it works fine and I can access the variable produced in that index view or whatever. Just need to know how to call the function once and have the variable accessible to all views since it will be in my header. I could put the information in a session variable but I read somewhere you should limit session variables to ids and generate the other needed information from those ids.

Thanks in advance,

  • 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-24T06:23:13+00:00Added an answer on May 24, 2026 at 6:23 am

    Just add a line like this to your application_controller.rb file and you will have access to current_car in your views.

    helper_method :current_car
    

    If you really wanted an instance variable, you could put a before_filter in your application as follows. Then both your controllers and views will have access to the @current_car variable.

    class ApplicationController < ActionController::Base
      before_filter :get_current_car
      def get_current_car
        @current_car = CarInfo.find(session[:car_info_id])
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an application and a user must log-in before he/she can access pages.
I want to get all the classes in my project userLocation variable. How can
I have 4 classes i.e views in my application. Class A, having variable a
I am building a multi-user web application. Each user can have their own site
I have a JavaScript, which returns 2 variables. I just want to access those
I have a variable last_login and want to output a message if the last
I have a GUID variable and I want to write inside a text file
i have a variable like the following and i want a function to only
I have a variable newItem. I want to place the value stored in newItem
i have a variable [ Dim mytime = 12:30:00 AM ] i want to

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.