How to Find the Local Port a Rails Instance Is Running On

How to tell which port Rails is running on in an initializer

Based on the lack of answers here and this thread on rubyforum: http://www.ruby-forum.com/topic/196017#new , I think that
there probably isn't a standard way to tell the port.

Rails: explicitly tell rails the port it's running on

Rails is supposed to create relative links, which do not include the fully qualified domain name, see example here, I quote :

link_to "Profile", profile_path(@profile)

# => <a href="/profiles/1">Profile</a>;

the *_url shortcuts, on the other hand, are using the full path, which is bad when you're behind a reverse proxy (that's apache in your case).

Have you tried using only *_path for links, and *_url for redirects, as, in the case of a redirect, apache should rewrite the url to match its port (assuming you're using the ProxyPass and ProxyPassReverse directives at Apache level) ?

If you use url_for, you can force it to be relative by setting the :only_path to true.

If your apache configuration is not based on ProxyPass, could you please copy/paste the interesting part so I can fix this answer ?

Thanks in advance.

Server is already running in Rails

Remove the file: C:/Sites/folder/Pids/Server.pids

Explanation
In UNIX land at least we usually track the process id (pid) in a file like server.pid. I think this is doing the same thing here. That file was probably left over from a crash.

Getting the server port outside a Controller

The reason you cant access request.port is that the request object is tied to a clients request which does only exist when you look at a specific request. What you want is accessing the webservers configuration. Thats a completely other thing.

You might solve this looking up the pid of the webserver which is possible by accessing the applicationwide $$ variable which contains the pid of the process executing the ruby code. Then you can lookup the port used by this process. You can du this using the system command which executes external applications. Here is a post that explains how ou can identify a port that belongs to a process:

http://www.cyberciti.biz/faq/what-process-has-open-linux-port/

This will work when there is only one process that is running the whole application. When using a combination of thin and nginx this wont work because the process running the code isn't the process thats running the webserver.

Rails server says port already used, how to kill that process?

Assuming you're looking to kill whatever is on port 3000 (which is what webrick normally uses), type this in your terminal to find out the PID of the process:

$ lsof -wni tcp:3000

Then, use the number in the PID column to kill the process:

$ kill -9 PID

How do I set the port correctly in email links during a system test?

The URL used in mailers is specified by:

Rails.application.configure.action_mailer.default_url_options

In config/environments/test.rb I had set mine to port 3000 when I first installed Clearance:

config.action_mailer.default_url_options = {host: "localhost:3000"}

To fix it, I first tried specifying the port dynamically but the suggested method didn't actually work and it seems it isn't necessary. Removing the port number was enough to get my system test passing:

config.action_mailer.default_url_options = {host: "localhost"}

As mentioned by Thomas Walpole, the reason this works is that Capybara.always_include_port is set to true. With this setting, attempts to access http://localhost/confirm_email/<token> (with no port specified) are automatically rerouted to http://localhost:<port>/confirm_email/<token>.

The always_include_port setting defaults to false in the Capybara gem but it turns out Rails sets it to true when it starts up the System Test server.



Related Topics



Leave a reply



Submit