Set ssl_allowed / ssl_required for all pages in Rails 2?

I found that AJAX calls did not work on my Rails site when the page used SSL / HTTPS. I worked on this by adding

ssl_allowed :action1, :action2, :actionN 

for the involved controllers.

I predict that this is pain and a tendency to make mistakes in the future, as I will no doubt forget to add the action to the ssl_allowed list.

Is there a way to enable ssl_allowed / ssl_required globally in the [ssl_requirement] [1] gem for all actions of each controller on my site? I tried to add the following to ApplicationController, but this did not work:

 ssl_allowed :all 
+1
source share
2 answers

I found a coarser ssl_requirement fork in github ( link ) that allows "ssl_allowed: all" and replaced my copy of the gem with this version. Now I use "ssl_allowed: all" in my ApplicationController and nowhere else. Exactly what I wanted.

+2
source

If you prefer not to depend on a forked plugin, can you override ssl_allowed? in your controller:

  class ApplicationController < ActionController::Base ... private def ssl_allowed? true end end 

EDIT: It does not do what I thought. Instead of disabling http redirects for pages that are not ssl_required, it shortens the entire redirect process so that nothing is done. This is very bad. The code:

  def ensure_proper_protocol return true if ssl_allowed? if ssl_required? && !request.ssl? redirect_to "https://" + request.host + request.request_uri flash.keep return false elsif request.ssl? && !ssl_required? redirect_to "http://" + request.host + request.request_uri flash.keep return false end end 

Adding ssl_allowed? a method like this would only be the answer if I read the code instead:

 def ensure_proper_protocol if ssl_required? && !request.ssl? redirect_to "https://" + request.host + request.request_uri flash.keep return false elsif request.ssl? && !ssl_required? && !ssl_allowed? redirect_to "http://" + request.host + request.request_uri flash.keep return false end end 
+2
source

All Articles