简体   繁体   English

在Rails 5中为特定的控制器禁用force_ssl

[英]Disable force_ssl for specific controllers in Rails 5

I added force_ssl to my production.rb: 我在我的production.rb文件中添加了force_ssl

  config.force_ssl = true

But now I want to disable SSL for some of my controllers. 但是现在我想为某些控制器禁用SSL。 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 PagesController:0x4a29560的未定义方法`force_no_ssl'

Is there a way to do it? 有办法吗?

You're using the global configuration method. 您正在使用全局配置方法。 This ensures ssl on every controller and every action. 这样可以确保每个控制器和每个动作都使用ssl。 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. 您可以将其添加到所需的每个控制器中,也可以将其添加到应用程序控制器中,然后根据控制器/操作组合将其关闭,我喜欢一个case语句,因为它允许多个选项,但是您可以做得最好为您的应用。

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 : 我的解决方案与上述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 : 然后,我在其中添加所有应该使用http的控制器:

before_action :force_no_ssl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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