In a Rails Model, I want to be able to find out the host and port. For example, if I am in a test environment it would return http://localhost:3000/
and if I was in production it would return something like http://my.application.com/
?
You can call Rails::Server. new. options[:Port] to get the port that your Rails server is running on. This will parse the -p 3001 args from your rails server command, or default to port 3000 .
Double-click the "Hosts" file in the System 32\drivers\etc folder, then select "Notepad" from the "Open with" list. The file will open in Notepad. Scroll down to the bottom of the file until you see: "127.0. 0.1 localhost." Replace the word "localhost" with the name you prefer.
For Rails 5.0 and above, you can use this command Show activity on this post. Use ctrl+c to shutdown your Webrick Server.
"#{root_url}"
which should be declared in your routes.rb
host_with_port
should work for you: http://apidock.com/rails/ActionDispatch/Http/URL/host_with_port
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