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 8233945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:18:29+00:00 2026-06-07T18:18:29+00:00

I would like to declare a variable in my application_controller.rb controller that is accessible

  • 0

I would like to declare a variable in my application_controller.rb controller that is accessible to all controllers that inherit from it. if possible I would like the variable only accessible in the child classes, nowhere else, including the views (unless specifically passed into the view).

I am new to Ruby and Rails and am unsure if the “protected” scope exists for variables, I have seen that it does for functions. I have not been able to find a simple answer and I have been experimenting a little in my app with different ways to declare variables and where they can be accessed. That has provided me with no information about how I can accomplish this.

Any help would be greatly appreciated.

Code:

class ApplicationController < ActionController::Base
    protect_from_forgery
    @admin_name = "AdminUserName"
    @admin_password = "AdminPassword"
end

class ProjectsController < ApplicationController
    http_basic_authenticate_with :name => @admin_name, :password => @admin_password, :except => [:index, :show]

    # controller functions here
end

This does not seem to be working for me.

  • 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-06-07T18:18:31+00:00Added an answer on June 7, 2026 at 6:18 pm

    As you have recognized, something like a protected scope does not exist for variables in ruby. You can use an instance variable in Rails to set variables in controllers that are accessible to views. It’s an old Rails feature that you can use instance variables set in the controller in the views.

    Instance variables get inherited from instance to instance

    class A
      def make_ivar
        @foo = 'bar'
      end
    end
    
    class B < A
      def get_ivar
        @foo
      end
    end
    
    b = B.new
    b.make_ivar
    b.get_ivar #=> @foo
    

    But be aware that by passing instance variables to views rails is breaking encapsulation and using it through all partials may not be good practice. Best of all, replace instance variables with local variables as soon as they land in the views

    UPDATE

    In your case, use constants. Constants are under the scope of the class they have been defined in and get inherited, but they are not available to the views unless called with scope

    class ApplicationController < ActionController::Base
      protect_from_forgery
      ADMIN_NAME = "AdminUserName"
      ADMIN_PW = "AdminPassword"
    end
    
    class ProjectsController < ApplicationController
      http_basic_authenticate_with :name => ADMIN_NAME, :password => ADMIN_PW, :except => [:index, :show]
    
      # controller functions here
    end
    

    I guess you do not want to call them in the view. If you really wanted to do that, you can do it like this:

    ApplicationController::ADMIN_NAME
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to declare a global variable that takes value from DataStore and
I would declare an empty String variable like this: string myString = string.Empty; Is
i would like to do this: DECLARE @TmpTable TABLE = select * from someTable
I would like to know how do I declare a record, that has some
Got a complex SELECT query, from which I would like to insert all rows
I would like to declare a variable 'XMLOutput' and have it produce the contents
I would like to declare a variable within an if/else statement in a SQL
I would like to declare some integer constants in PowerShell. Is there any good
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like a for loop in jquery so that: For every hover_link: show hidden

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.