简体   繁体   中英

How does rails 4 generate _url and _path helpers

I am confused about how Rails 4 generates urls using _url and _path helpers. My configuration is I have a hardware device handling https requests which routes to nginx serving static files and referring rails requests to a thin server.

In my config/environments/production.rb i have

config.action_controller.default_url_options = {host: "mydomain", protocol: 'https'}

If I issue a redirect in a controller using an `_path helper it tries to redirect to the nginx port. (which is not accessible externally) If I issue a redirect in a controller using an _url helper it redirect correctly according to the config.

In a view a link_to _path and _url generate the correct url

So I have two questions Why do the helpers work differently in a view and in a controller. Is there a way to generate the correct url using _path or should I change everything to _url - and is there a downside to that?

thanks in advance

Andy

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.

I guess this is because _path generates a relative path whereas _url generates the complete url.

Now, when you are in the controller, _path helper will try to redirect to the nginx port because it is where the request is being processed. Whereas, when you are in a view, the relative path works just fine.

I recommend that you change to _url in controllers.

Also, these methods are created automatically when routes are defined and in the case of RESTful routes, they follow a predictable convention.

Running 'rake routes' is a helpful way of seeing all of the routes being generated.

I recommend you read http://guides.rubyonrails.org/routing.html for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM