Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between _url and _path while using the routes in rails

When we define routes in routes.rb using the name like map.some_link.We can use the link in two ways- some_link_url, some_link_path.

  • What are the differences between the two?
  • Which is more secure to be used?
like image 857
MohamedSanaulla Avatar asked Feb 28 '10 08:02

MohamedSanaulla


People also ask

What is the difference between path and route?

A route can be part of a path when only a section of the path is actually traveled. A path is always physical available and may end in a rather unclear manner (e.g A footpath in a forest that blurs away). One or more paths can be part of a route.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How do routes work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.


2 Answers

I had the same question and I wrote a small post about this in my blog

The reason is summarized here (I found this on a forum):

*_path are for views because ahrefs are implicitly linked to the current URL. So it’d be a waste of bytes to repeat it over and over. In the controller, though, *_url is needed for redirect_to because the HTTP specification mandates that the Location: header in 3xx redirects is a complete URL.

Here is another explanation which says it depends on whether we need to use an absolute URI when linking to an SSL site from a non-SSL site, and vice versa.

What I have read so far, doesn't suggest that any of them is more secure than the other. It really comes down to what is the "proper" usage.

like image 98
Petros Avatar answered Oct 16 '22 01:10

Petros


path is relative while url is absolute.

like image 25
ponzao Avatar answered Oct 16 '22 01:10

ponzao