【问题标题】:Redirect all pages except 1 to a single Temporary Unavailable Page in Rails将除 1 之外的所有页面重定向到 Rails 中的单个临时不可用页面
【发布时间】:2014-08-27 07:21:51
【问题描述】:

我们有一个功能齐全的网站,但后端正在停机进行维护,因此我们希望将所有客户重定向到 www.example.com/unavailable。除了 1 页 www.example.com/admin,因为这是我们打开和关闭不可用页面的管理页面。

所以我的问题: 是否可以像这样打开和关闭路线:

routes.rb 的示例代码:

  if Settings.unavailable
    get "*", to: "/unavailable", except: "/admin"
  end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 routes


    【解决方案1】:

    在 Michal 帖子的帮助下,我将他的回答改编为以下内容:

    class ApplicationController < ActionController::Base
      before_filter :check_maintenance
    
      def check_maintenance
        if Settings.first.maintenance && request.fullpath.split("?")[0].gsub("/","") != 'unavailable' && (request.fullpath.split("?")[0].exclude? "admin") # or other conditions
          redirect_to '/unavailable'
        end
      end
    end
    

    【讨论】:

      【解决方案2】:

      您可以在您的 routes.rb 中添加条件,但您将无法检测到当前 url 是什么。您应该将此逻辑放在 ApplicationController(或其他顶级控制器)的 before_filter 中:

      class ApplicationController < ActionController::Base
        before_filter :check_maintenance
      
        def check_maintenance
          if Settings.unavailable && action_name != 'unavailable' # or other conditions
            redirect_to '/unavailable'
          end
        end
      end
      

      【讨论】:

      • 就像你说的我需要更多条件,因为现在我要进入一个无限循环。
      • 感谢您的帮助,请注意我的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      相关资源
      最近更新 更多