Search code examples
ruby-on-railsruby-on-rails-5

How to fetch base URL in rails?


I need the base url of my rails app in its code. Example if my site is www.abc.com then i want this to be accessible in all controllers. Is there any method to get it?


Solution

  • There are two step to archive :

    1. According to documentation, you can use request.host or request.host_with_port if you also need the port

    2. You can use environment variable to store your BASE_URL, so that when you need to get your base url, you can call it like ENV['BASE_URL'] either in the model, controller or view

    You can use figaro gem to help you manage your environment variable