Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Rails URL helpers to automatically output https urls

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?

like image 296
Joe Van Dyk Avatar asked Jul 14 '11 21:07

Joe Van Dyk


2 Answers

Use test_url(:protocol => 'https') for https urls.

like image 123
Krishna Srihari Avatar answered Oct 18 '22 14:10

Krishna Srihari


Haven't tried but add this in your ApplicationController:

def default_url_options(options={})
 { :secure => true }
end 
like image 32
apneadiving Avatar answered Oct 18 '22 14:10

apneadiving