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/"
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.
Take advantage of ruby's inheritance chain. You can define that on some parent class of all your controllers as a constant, normally ApplicationController
:
class ApplicationController < ActionController::Base
BASE_URL = "www.url.com/something/"
end
Then it will become available to all is children, namely PostsController < ApplicationController
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With