Search code examples
ruby-on-railsruby

Get hostname of URL in ruby on rails


I am making rails app and I need to get just hostname of my URL from my one of Rails controllers.

If my URL is http://www.example.com/path/0,then I just want to extract www.example.com part. How can I do this? I found request.base_url but this returns http://www.example.com which I do not want.

In javascript there is a function, window.location.hostname. I wonder there is a equivalent in Ruby on Rails.


Solution

  • You can use request.host to get exact your URL which you want.

    And you can use request.port to get your port from url