简体   繁体   中英

Disable force_ssl for specific controllers in Rails 5

I added force_ssl to my production.rb:

  config.force_ssl = true

But now I want to disable SSL for some of my controllers. I tried to add to my controllers:

before_action :force_no_ssl

but it gives me an:

undefined method `force_no_ssl' for PagesController:0x4a29560

Is there a way to do it?

You're using the global configuration method. This ensures ssl on every controller and every action. Switch to controller based forcing.

You can either add it to every controller you want it in, or add it to the application controller and turn it off based on the controller/action combo, i like a case statement because it allows multiple options, but you can do what works best for your app.

class ApplicationController < ActionController::Base
  force_ssl unless: :no_ssl?

  def no_ssl?
    case "#{params[:controller]} #{params[:action]}"
    when "parents index"
      return false
    else
      return false
    end
  end
end

My solution is similar to that from above posted by trh :

def force_no_ssl
  if request.ssl? && is_production?
    redirect_to :protocol => 'http://', :status => :moved_permanently
  end
end

where I then add to all the controllers that should use http :

before_action :force_no_ssl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM