In short:
_url
Gives the entire path (domain name and protocol).
_path
Gives the path after the '/' (no domain name or protocol).
There are various reasons why you'd use one over the other.
In longer form you can read this page: http://guides.rubyonrails.org/routing.html . There is more information there that could help you here but it is quite well written and my attempts at summarizing things would probably short you of important details.
To address your questions directly:
- Why do the helpers work differently in a view and in a controller?
Probably better answered by http://guides.rubyonrails.org/routing.html than by me (I don't know exactly what you're trying to do).
You could also read this:
*_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.
. . . As posted here: https://stackoverflow.com/a/2350837/1026898
- Is there a way to generate the correct url using _path or should I change everything to _url?
Use your judgement based on the information provided above to make a decision. Are you trying to redirect to another domain (I suspect not)? If you are, you want to use '_url' over '_path'. If you are not, you probably want to use _path
. It's hard to say without knowing your exact case.
- Is there a downside to that?
Well technically there's probably some additional overhead if you do it one way or the other that you could avoid if you did it the other way. The real problem would arise, however, only when another developer comes in and has to make changes behind you. If you are using a weird solution (a solution different than the intended use of the helpers) it would be strange for them to figure out why you did things the way you did them.