Rails: how to get the current host, or build a URL with it? [duplicate] Rails: how to get the current host, or build a URL with it? [duplicate] ruby ruby

Rails: how to get the current host, or build a URL with it? [duplicate]


I often use a before_filter in ApplicationController to set an instance variable with the host name, something like this:

@hostname = request.host || "www.mydomain.com"

You can also use request.port to get the port number which the request came through (taken from the HTTP headers).


Basically you need to generate url based on the current url and some other port number.it can be done as -

request.url(:port => 20)

On rails console

app.root_url(:port => 20) => "http://www.example.com:20/" 

To extract host from url use

request.host