Getting Rails URL helpers to automatically output https urls

Viewed 30320

I am developing a site that mixes http and https a lot - whats the best/easiest way to make the links use the right protocol for the route - can it be specified in the routes file?

Say I have the following route in Rails 3.

match "/test" => "test#index", :as => :test, :constraints => { :protocol => 'https' }

If I'm on a http page, and I use test_url(), it'll output http://domain.com/test. I want https://domain.com/test instead.

I know I can use test_url(:secure => true), but that's duplicating logic.

I know I could have http://domain.com/test to https://domain.com/test, but that's an extra redirect, plus it fails on form posts.

Ideas?

8 Answers

To generate https url, add an option called protocol with value https

test_url(protocol: 'https')
Related