Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the host and port in a Rails application

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/?

like image 226
Jonathan Avatar asked May 11 '10 18:05

Jonathan


People also ask

How do you know on which port rails is running?

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 .

How do I change my local host number?

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.

How do you close a rails server?

For Rails 5.0 and above, you can use this command Show activity on this post. Use ctrl+c to shutdown your Webrick Server.


2 Answers

"#{root_url}" 

which should be declared in your routes.rb

like image 182
Kamilski81 Avatar answered Sep 19 '22 17:09

Kamilski81


host_with_port should work for you: http://apidock.com/rails/ActionDispatch/Http/URL/host_with_port

like image 36
yiwen Avatar answered Sep 20 '22 17:09

yiwen