Path and Url

Hello everyone, what’s the real difference between root_url and root_path in Rails, for example. Apparently both lead to the same route in the application.

Thanks!

My understanding is that one is a full url, the other is a path. Generally you'd use a path, but if you are sending an email or doing something else that needs a full URL, you'd use the _url.

Best Wishes, Peter

They are both methods that return a string so it is easy to find out. <%= "root_path = #{root_path}, root_url = #{root_url}" %>

Colin

Hi there,

With *_url you'll get the full path with protocol and domain name. Use it for redirect method for example, or when you redirecting for total different domain.

With *_path you will get the part from your path which is after '/' without domain and stuff. Use it every other situation I think.

With redirecting _path will also work fine, just later it can cause problems when you need to refactor your code. And an easy link_to can be used with _url, but it fills your HTML unnecessary characters, which also not a good idea.

Check both, see the source of your HTML and you will see easily the diff.

good luck, gezope

Thank you guys. I checked it out and it makes total sense.

Cheers!

One other thing to look at is using _url whenever you need to cross the secure / non-secure bridge (either way). Also, any time you are inside JavaScript (Ajax) requests, be sure to use _path, otherwise you may trigger a same origin policy error in browsers that care about security.

Walter