code-quality

How to Get Current Host Url in Rails

how to get root url along with port number in rails

How to Get Current Host Url in Rails

You are in right place if you want to get root url along with port number in rails.

Code that works

This can be achieved by either root_url or request.env[HTTP_HOST].

Use Case

In applications you may need to generate url that will link back to your application while sending email.

Problems it has solved:

Previously, we used to have environment variable like HOST: localhost:3000 saved in application.yml which would be later called by ENV['HOST'].

Press Esc to return to the article
About the author

Prakash Poudel Sharma

Independent product manager & agentic engineering consultant

I'm an independent product manager, founder, and software builder based in Kathmandu. I help teams make product decisions and adopt agentic software development, drawing on over a decade of building software. I write about product thinking, engineering with AI, and hands-on experiments.

Keep reading

More on this

Join the conversation0 comments

What did you take away?

Thoughts, pushback, or a story of your own? Drop a reply below — I read every one.

Comments are powered by Disqus. By posting you agree to theirterms.

—
0:000:00