sslruby-on-rails-4high-voltage

Select SSL Routes serving up rails 4 static pages via highvoltage gem


I have several static erb pages being served up in a ruby rails 4 site via the high voltage gem:

get '/about'    => 'high_voltage/pages#show', id: 'about'
get '/contact'  => 'high_voltage/pages#show', id: 'contact', :protocol => "https"
get '/privacy'  => 'high_voltage/pages#show', id: 'privacy'

This all works well and good, except that the /contact route doesn't redirect or force SSL on, it is happy with whatever protocol is used.

I host the site on engine yard, attempting to put :force_ssl only or variants in the route line resulted in failed deployments - high voltage uses a slightly different set of arguments than normal routes so I suspect there is a conflict somewhere.

Anyone use highvoltage and SSL with rails 4 for select static pages (not the whole site)? Example routes line please.


Solution

  • You can achieve this by overriding the HighVoltage#PagesController see the override section of the documentation.

    It might look something like this:

    class PagesController < ApplicationController
      include HighVoltage::StaticPage
    
      before_filter :ensure_secure_page
    
      private
    
      def ensure_secure_page
        if params[:id] == 'contact'
          # check to make sure SSL is being use. Redirect to secure page if not.
        end
      end
    end
    

    Next disable the routes that HighVoltage provides:

    # config/initializers/high_voltage.rb
    HighVoltage.routes = false
    

    Then in your application's routes file you'll need to set up a new route:

    # config/routes.rb
    get "/pages/*id" => 'pages#show', as: :page, format: false