Difference between _path and _url in rails and when to use them…?

You would have been using redirect_to root_path in your controller action. We also have used redirect_to root_url. So are they same or are they different. _path and _url have a small technical difference but can be a big blow when used wrongly in your application without knowing the actual purpose both render.

Technical difference between url and path

users_path  # => /users

users_url # => http://localhost:3000/users

So basically, _path is relative and _url is absolute. Meaning, _path talks to app, which in return talks to browser using routes protocol but url talks directly to the browser using routes protocol. The above example shows clearly the result of the both.

In the example above, users_path renders /users page through which the browser loads the page whereas users_url renders absolute url http://localhost:3000/users to browser directly.

When to use _url and _path…?

Use _path in the view

_path strictly need to used in the view, especially for form submission. _path is highly recommended while using simple_form or form_tag for form submission. Using _path with redirect_to in the controller is recommended too.

<%= form_tag export_invoice_path, method: :post do %>
  ...
  <% submit_tag "Save", class: "btn_save"%>
<% end %>

In the above example, for form submission, _path is highly recommended. As it renders URL through app not directly to the browser.

Use _url for external links usage

We highly recommend to use when providing links for external use like in emails when creating links to the app on the server. Also _url can be used while making API calls to the routes within the app.

<%= link_to 'Verify Email Address', newsletter_subscription_url, style: "background-color:#ff8500; padding: 15px 20px; color:#fff; %>

In the above example, we use _url for newsletter verification which comes to the email inbox. So _url provides links directly without talking to the app.

If you use _url in the form_tag there might be authentication issue as _url does not talk to the app, but just renders the required URL/page to the browser. So the submitted form may not hit the controller action due to authentication issue, as the form submission is happening through browser, not the app.

def index
  @object = Object.all
  redirect_to users_path # => /users
end
def index
  @object = Object.all
  redirect_to users_url # => http://localhost:3000/users
end

Using _path or _url in controller action for redirection causes no harm.

One Response to “Difference between _path and _url in rails and when to use them…?

  • DarrellNen
    5 years ago

    Thanks for helping people find the info they need. Good stuff as always. Keep up the great work!!!
    [url]https://history-essay.weebly.com/skills-revealed.html[/url]
    [url]https://esay-h.weebly.com/third.html[/url]
    [url]https://h-essay.weebly.com/third.html[/url]
    [url]https://histss.weebly.com/second.html[/url]
    [url]https://hirtesy.weebly.com/thrird.html[/url]

Leave a Reply

Your email address will not be published. Required fields are marked *