Question
In a Ruby on Rails view, how can I get the current absolute URL instead of only the relative path? For example, request.request_uri returns only the relative URL, but I need the full URL including the protocol, host, and path.
Short Answer
By the end of this page, you will understand how Rails represents request URLs, how to get the full current URL in a view or controller, and when to use methods like request.url, request.original_url, request.path, and request.fullpath. You will also see common mistakes and practical examples.
Concept
In Rails, the request object gives you information about the current HTTP request. This includes the host, protocol, path, query string, headers, and more.
When people ask for the current absolute URL, they usually mean the full address of the current page, such as:
https://example.com/products?page=2
That full URL contains:
- Protocol:
https:// - Host:
example.com - Path:
/products - Query string:
?page=2
A relative URL only includes the path and maybe the query string, for example:
/products?page=2
In Rails, different request methods return different parts of the URL:
request.url→ full absolute URLrequest.original_url→ original full URLrequest.path→ path onlyrequest.fullpath→ path plus query string
This matters because using the wrong method can lead to bugs. For example:
- generating incorrect redirect targets
- sharing incomplete links
- storing the wrong page location for analytics
- creating broken callback or return URLs
If you need the full current URL in a Rails view, is usually the direct answer.
Mental Model
Think of a URL like a postal address.
- Protocol is the delivery method:
httporhttps - Host is the city and street:
example.com - Path is the apartment number:
/products - Query string is extra delivery instructions:
?page=2
Rails lets you ask for different parts of that address.
request.pathasks for just the apartment numberrequest.fullpathasks for apartment number plus instructionsrequest.urlasks for the complete mailing address
So if you want the whole address, ask Rails for the whole address, not just one part of it.
Syntax and Examples
The most common way to get the current absolute URL in Rails is:
request.url
In a view:
<p>Current URL: <%= request.url %></p>
Example output:
Current URL: https://example.com/products?page=2
You can also use:
request.original_url
Example:
<p>Original URL: <%= request.original_url %></p>
Common request URL methods
request.url
request.original_url
request.path
request.fullpath
request.host
request.protocol
Example showing the difference
<ul>
<li>URL: <%= request.url %></li>
<li>Original URL: <%= request.original_url %></li>
<li>Path: <%= request.path %></li>
<li>Full path: <%= request.fullpath %></li>
<li>Host: <%= request.host %></li>
<li>Protocol: <%= request.protocol %></li>
</ul>
If the current page is:
https://example.com/shop/items?category=books
You would get roughly:
request.url→
Step by Step Execution
Consider this code in a Rails view:
<%= request.url %>
Suppose the browser visits:
https://myapp.com/articles/5?preview=true
Here is what happens step by step:
- The browser sends a request to Rails for
/articles/5?preview=true. - Rails builds a
requestobject containing details about that HTTP request. - In the view,
requestis available automatically. - Calling
request.urltells Rails to return the full absolute URL. - Rails combines the protocol, host, path, and query string.
- The view outputs:
https://myapp.com/articles/5?preview=true
Step-by-step comparison
<p><%= request.path %></p>
<p><%= request.fullpath %></p>
<p><%= request.url %></p>
For the same request, Rails evaluates them like this:
request.path→/articles/5request.fullpath→/articles/5?preview=true
Real World Use Cases
Getting the full current URL is useful in many real applications:
- Share links: show users the exact page URL to copy and share
- Return after login: remember where the user came from before authentication
- Analytics and tracking: log the exact page being viewed
- Webhook or callback setup: build links that must include the correct host and protocol
- Debugging: display the current request URL during development
- Canonical links: generate SEO-related page URLs when needed
Example: storing a return URL
session[:return_to] = request.url
Example: showing a share link in a view
<input type="text" value="<%= request.url %>" readonly>
Example: logging
Rails.logger.info("User visited: #{request.url}")
Real Codebase Usage
In real Rails codebases, developers often use request URL methods in a few common patterns.
Guard clauses and redirects
A controller may save the current URL before redirecting a user:
unless current_user
session[:return_to] = request.url
redirect_to login_path
end
This works well because the full URL can later be used to send the user back to the same page.
Validation and safety
Developers are careful when reusing URLs from requests, especially for redirects. A stored URL should be validated if there is any chance it came from user-controlled input.
redirect_to(session[:return_to] || root_path)
Logging and monitoring
Applications often include the current URL in logs or error reports:
Rails.logger.warn("Unexpected state at #{request.url}")
Building links with request data
Sometimes code needs only part of the URL, such as the host or protocol:
base_url = "#{request.protocol}#{request.host_with_port}"
This is common when generating absolute links in emails, APIs, or background jobs.
Common Mistakes
A common beginner mistake is using a path-only method when a full URL is needed.
Mistake 1: Using request.path for a full URL
Broken example:
request.path
Output:
/products
Why it is wrong:
- It does not include protocol or host.
- It may not be enough for redirects, emails, or external links.
Use instead:
request.url
Mistake 2: Forgetting the query string
Broken example:
request.path
If the real page is:
https://example.com/search?q=rails
You lose ?q=rails.
Use instead:
request.fullpath # relative with query string
request.url # full absolute URL with query string
Mistake 3: Rebuilding URLs manually without the port
Broken example:
Comparisons
Here is a practical comparison of common Rails request URL methods:
| Method | Returns | Includes host? | Includes query string? | Typical use |
|---|---|---|---|---|
request.path | Path only | No | No | Routing logic, path checks |
request.fullpath | Path + query string | No | Yes | Preserve filters/search params |
request.url | Full absolute URL | Yes | Yes | Share links, logging, redirects |
request.original_url | Original full URL | Yes |
Cheat Sheet
# Full current absolute URL
request.url
# Full original URL
request.original_url
# Path only
request.path
# Path + query string
request.fullpath
# Host only
request.host
# Host with port
request.host_with_port
# Protocol only
request.protocol
Quick rules
- Use
request.urlwhen you need the full URL. - Use
request.pathwhen you only need/some/path. - Use
request.fullpathwhen you need/some/path?x=1. - Use
request.host_with_portinstead ofrequest.hostif the port matters.
Example
If the page is:
http://localhost:3000/posts/1?draft=true
Then:
request.url # => "http://localhost:3000/posts/1?draft=true"
request.original_url # => "http://localhost:3000/posts/1?draft=true"
request.path # => "/posts/1"
request.fullpath # => "/posts/1?draft=true"
request.host
request.host_with_port
request.protocol
FAQ
How do I get the current full URL in a Rails view?
Use:
request.url
What is the difference between request.path and request.url in Rails?
request.path returns only the path like /posts/1, while request.url returns the full absolute URL like https://example.com/posts/1.
Does request.url include query parameters?
Yes. If the current URL has a query string, request.url includes it.
When should I use request.original_url instead of request.url?
In many cases they are the same. request.original_url is useful when you specifically want the original complete request URL.
Why does request.request_uri not give the full URL?
Because it returns a relative form of the request location rather than the full absolute address with host and protocol.
Can I use request.url in both controllers and views?
Mini Project
Description
Build a small Rails page that displays different parts of the current request URL. This helps you see the difference between path-only methods and full absolute URL methods in a practical way.
Goal
Create a Rails page that shows request.url, request.original_url, request.path, and request.fullpath for the current request.
Requirements
- Create a controller action that renders a page.
- Display the current full URL using
request.url. - Display the path-only and path-with-query-string versions.
- Visit the page with a query parameter to compare the outputs.
Keep learning
Related questions
How to Call Shell Commands from Ruby and Capture Output
Learn how to run shell commands in Ruby, capture output, check exit status, and choose the right method for scripts and apps.
How to Check Whether a String Contains a Substring in Ruby
Learn how to check if a string contains a substring in Ruby using include?, match, and multiline string examples.
How to Check if a Hash Key Exists in Ruby
Learn how to check whether a specific key exists in a Ruby hash using key?, has_key?, and include? with clear examples.