Getting Absolute and Relative Paths of Current View in Rails
When using Ruby on Rails, this article introduces how to output the absolute and relative paths of the current view. This article targets Ruby on Rails 5, but if youโre using a different version or encounter errors, please refer to the official documentation.
How to Get Absolute Path
To get the absolute path, use request.original_url. This returns the complete URL.
# Absolute path
request.original_url # => https://www.example.com/users/sign_in
How to Get Relative Path
To get the relative path, use request.fullpath. This returns only the path portion without the hostname.
# Relative path
request.fullpath # => /users/sign_in
By using these methods, you can easily retrieve the URL information of the current view. This is often useful during Rails application development, so please make use of it.