Getting Rails Url Helpers to Automatically Output Https Urls

Getting Rails URL helpers to automatically output https urls

It looks like this will be solved in Rails 4! https://github.com/rails/rails/commit/9b4514c3b8ecfbc40a44dbd4c2ebd4ce67f4a459

How can I define multiple url helpers for same route entry in Rails?

@kunashir is mentioning the right thing -
if its necessary to have one route available through more named routes you need to define them individually.

But it can be DRY as follows:

[:dashboard, :signed_in_default].each do |helper|
get 'dashboard', as: helper, to: "dashboard#index"
end

How does rails 4 generate _url and _path helpers

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.

Rails 3 SSL routing redirects from https to http

If you want all your links to be able to switch between http and https, you have to stop using the _path helper and switch to _url helpers.

After that, using a scope with the protocol parameter forced and protocol constraint makes the urls automatically switch.

routes.rb
scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
resources :sessions
end

resources :gizmos

And now in your views:

<%= sessions_url # => https://..../sessions %>
<%= gizmos_url # => http://..../gizmos %>

Edit

This doesn't fix urls that go back to http when you are in https. To fix that you need to override url_for.

In any helper

module ApplicationHelper
def url_for(options = nil)
if Hash === options
options[:protocol] ||= 'http'
end
super(options)
end
end

This will set the protocol to 'http' unless it was explicitly set (in routes or when calling the helper).

Rails forms leading to Mixed Content warning

You have not correct protocol set up for your environment.

Your server is running on HTTPS and form creates HTTP URL.

You need to tell your URL helpers to build the URLs with the same protocol as your server is running for your environment.

Development should run on HTTP, staging should be HTTPS and production as well HTTPS.

There are different ways how to do it. The best is to set protocol in your environment config file. So place this line:

Rails.application.routes.default_url_options[:protocol] = 'https'

into your environment config file like production.rb and staging.rb.

Another approach is to set the default protocol per controller action. Check this one for more info.

In case you are using the mailer, also check your mailer protocol settings. As described here.

Rails 3 route question: use top level url based on model name not id

Add a new match statement as the last route in your routes.rb file (if you put it earlier, it'll be matched before other resources; and of course, make sure your shows resource is listed before the match statement):

resources :shows
match "/:id" => "tapes#show"


Related Topics



Leave a reply



Submit