Eduardo Pedroso
Eduardo Pedroso

Reputation: 879

Creating a global variable to all the controllers in Rails

I have a base URL common to all my controllers. I want to declare this as a variable in one place, and use it in all my controllers. That will make any future updates fast and simple. Is that possible? I'm declaring it in all my controllers like this:

@baseURL = "www.url.com/something/"

Upvotes: 6

Views: 12987

Answers (6)

ichigolas
ichigolas

Reputation: 7735

Typically, all your controllers will inherit from ApplicationController. You may then define the variable there, making it available to its descendants.

class ApplicationController < ActionController::Base
  BASE_URL = "www.url.com/something/"
end

Upvotes: 7

DarthTux
DarthTux

Reputation: 156

You could define a method inside ApplicationController and use that method like a helper_method to access it from the views.

class ApplicationController < ActionController::Base

  helper_method :base_url
  def base_url
    @base_url ||= "www.url.com/something/"
  end
end

I try to avoid before_actions to setup variables. Inside your controllers and views you will able to call the base_url method.

It's the same to include this method in the application_helper.rb.

Upvotes: 3

Igor Springer
Igor Springer

Reputation: 496

If it is only one variable and you are sure, that you need it only within controllers scope declaring a constant in ApplicationController should be enough:

class ApplicationController < ActionController::Base
  BASE_URL = "www.url.com/something/"
end

class SomeOtherController < ApplicationController
  def index
   @base_url = BASE_URL
  end
end

However usually custom URLs (and other things like email addresses) earlier or later are needed in other parts of an application so it is useful to have a single source of truth by using gem like https://github.com/settingslogic/settingslogic and store all such variables in one place (file).

Upvotes: 0

guitarman
guitarman

Reputation: 3320

You could define a class variable in your application controller:

class ApplicationController < ActionController::Base
  @@baseURL = "www.url.com/something/"

  def self.baseURL
    @@baseURL
  end
end

class SomeFrontendController < ApplicationController

end

Within all your controllers now you could access @@baseURL or call the class method:

SomeFrontendController.baseURL
# => "www.url.com/something/"

But this is dirty. Better use a constant:

class ApplicationController < ActionController::Base
  BASE_URL = "www.url.com/something/"
end

class SomeFrontendController < ApplicationController

end

Within all your controllers now you could access BASE_URL or:

SomeFrontendController::BASE_URL

Upvotes: 0

abcm1989
abcm1989

Reputation: 81

Rails controllers inherit from ApplicationController. Try putting it there:

def baseUrl
 @baseURL = "www.url.com/something/"
end

Upvotes: 0

Alok Swain
Alok Swain

Reputation: 6519

In your application controller.

before_action :set_variables

def set_variables
 @baseURL = "www.url.com/something/"
end

This @baseURL instance variable will be accessible in all your actions and views as you make all controllers inherit the ApplicationController.

Upvotes: 10

Related Questions