0

In development, I would like to replace localhost:3000 to something like "domain.com:3000" or "domain.com". I can achieve this by adding alias to /etc/hosts file, for ex:

# /etc/hosts
127.0.0.1   domain.com

^^^ this one works, i can now view my site locally using domain.com.

The problem is that rails itself still uses localhost:3000, for example when generating urls via router methods. I feel that there should be some config for this.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
yerassyl
  • 2,958
  • 6
  • 42
  • 68
  • give this a try - https://github.com/bkeepers/dotenv – Caffeine Coder Sep 03 '19 at 08:35
  • Possible duplicate of [How do I set default host for url helpers in rails?](https://stackoverflow.com/questions/2660172/how-do-i-set-default-host-for-url-helpers-in-rails) – Dax Sep 03 '19 at 08:38

2 Answers2

4

You have to set:

Rails.application.routes.default_url_options[:host] = 'domain.com:3000'

in your development environment.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
1

Custom domain name for your IP Address can be created. First of all find your IP Address using ifconfig. Then open /etc/hosts file with sudo.

/etc/hosts file is in readonly, so need to open with sudo

sudo vi /etc/hosts

There you will find at least two entries for 127.0.0.1 and 127.0.1.1, below this, create your own domain name

<IP_Address> domain.com

Save the file. Then start your rails server and bind with your IP Address.

rails s -b <IP_Address>

And you are done.

In browser, <IP_Address>:3000 will be same as domain.com:3000