Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set root_url

Tags:

In my Ruby on Rails 3.1 app I have a link like this:

<%= link_to 'Home', root_url %> 

On My dev. machine it renders a link with "localhost:3000". On production it renders a link with an IP Address like this "83.112.12.27:8080". I would like to force rails to render the domain address instead of the IP Address. How can I set root_url?

like image 704
Robert Reiz Avatar asked Dec 15 '11 15:12

Robert Reiz


1 Answers

You are looking for ActionController's default url option. So you can do something like:

class ApplicationController < ActionController::Base   def default_url_options     if Rails.env.production?       {:host => "www.example.com"}     else         {}     end   end end 

This also works for ActionMailer. As well, both can be set in your environment .rb or application.rb

# default host for mailer config.action_mailer.default_url_options = {   host: 'example.com', protocol: 'https://' }  # default host for controllers config.action_controller.default_url_options = {   :host => "www.example.com" } 
like image 133
lucapette Avatar answered Sep 30 '22 09:09

lucapette