Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails force ssl only on specified controllers [duplicate]

I have a ssl certificate which serves https://secure.mydomain.com. I would like to use ssl only on certain controllers and not throughout my entire application. I have looked online and can't seem to find a complete and accurate tutorial on how to enable ssl for specified controllers.

I am aware that I can use the config ssl below but it does not serve me a purpose since it enables ssl site wide

config.force_ssl = true

How can I tell rails to only use ssl on a specific controller rather then my entire application?

like image 776
coletrain Avatar asked Jan 15 '23 07:01

coletrain


1 Answers

You can do this with constraints in routes.rb:

resource :account, :constraints => { :protocol => "https", :subdomain => "secure" }

Also, if you have many secure routes, and you want to DRY things up, you can create a scope for your secure routes:

scope :constraints => { :protocol => "https", :subdomain => "secure" } do

  ...[secure routes]...

end
like image 50
Sam Peacey Avatar answered Jan 16 '23 21:01

Sam Peacey