#Rails 4.2: How to create a full URL with given host and port

Basically if you need to generate url based on the current url during a request OR you can create your own Urls by using Rails ‘ActionDispatch::Integration::Session‘ class.

Rails creates an object ‘app’. It is and action dispatch session object.

You can make use of that object for creating your own URLs like:

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

> app.root_url(:port => 20, :host => 'www.bing.com')
=> "http://www.bing.com:20/"

During a request you can use like this:

> request.url(:port => 20)