2

What's the proper way to enforce https in a rails 4 route?

For example I would expect to be able to do something like:

get 'success' => 'ssl#success', :ssl_only => true

But that doesn't do anything.

pguardiario
  • 53,827
  • 19
  • 119
  • 159

4 Answers4

4

You can use force_ssl to force http://site/success to redirect to https://site/success. Please refer to the documentation for force_ssl.

class SSLController < ApplicationController
  force_ssl only: :success # see docs for more options
end

(Related question.)

Community
  • 1
  • 1
James Lim
  • 12,915
  • 4
  • 40
  • 65
1

If you want https://.../ssl/success

 scope constraints: { protocol: 'https' } do
  get 'success', to: 'ssl#success', as: 'success' 
 end

or

get 'success', to: 'ssl#success', as: 'success', constraints: { protocol: 'https' }
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
0

I eventually got this to work with:

get 'success', to: 'ssl#success', constraints: {protocol: /https/}
pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • hope it is same as my post ...excpt 'https' to /https/ – Rajarshi Das Sep 23 '13 at 07:46
  • I know, it's a small thing. My hunch is that the protocol is actually 'https://' or something and this is why the regex works but not the string. – pguardiario Sep 24 '13 at 01:14
  • Doesn't work for you maybe, but his answer is the correct one and it lead you to your answer. I think that would have been fair. – Ely Feb 16 '15 at 05:43
0

In my case it worked by adding "force_ssl" in the application_controller.rb.