【问题标题】:Rails 3, redirect from https to https, overwrite _path helperRails 3,从 https 重定向到 https,覆盖 _path 助手
【发布时间】:2011-11-26 21:32:04
【问题描述】:

我想要混合 https/http 站点。

此外,我希望从 https 重定向到 http(即,在用户成功登录后,它应该重定向到 http 的根页面)。

宝石喜欢:

  • 机架-ssl
  • rack-ssl-enforcer

完美运行,但前提是您想在 https 上拥有整个站点 “混合 http/https”,在 A、B、C 操作中只有 ssl,在 D、E、F 中只有 http - 不起作用。

我从另一个 SO 线程检查了解决方案:

Rails 3 SSL routing redirects from https to http

几乎可以工作。 它很容易编写脚本,它将(在整个视图上)助手从“_path”更改为“_url”。

但是这样的链接有问题:

<%= link_to "model", some_model %>
<%= link_to "edit model", edit_mode_url(model) %>
...

有许多不同的模型,我经常在迭代块中使用“模型”,因此基于“重写”脚本的解决方案将无法使用。

问题:

有没有办法改变&lt;%= link_to 'model', model %&gt; 代码的行为来解决这个问题?是否有可能覆盖路径助手(标准协议将是 http,在给予者参数上 - https)?

或者我还没有找到其他解决方案?

编辑:

我使用 Rails 3.0.9。

【问题讨论】:

    标签: ruby-on-rails-3 http redirect ssl https


    【解决方案1】:

    如果您想将 https 添加到特定路由

    你可以使用这个代码

    before_filter :redirect_to_https
    def redirect_to_https
         redirect_to :protocol => "https://" unless (request.ssl? || request.local?)
    end
    

    您可以通过执行以下操作来定义要与 before_filter 操作一起使用的路由

    before_filter :redirect_to_https, :except => [:action1 , :action2]
    before_filter :redirect_to_https, :only => [:action1 , :action2]
    

    【讨论】:

      【解决方案2】:

      使用这个宝石:

      https://github.com/retr0h/ssl_requirement

      gem install ssl_requirement

      然后添加ssl_required :new, :destroy #others actions 到你的控制器。

      如果您使用 devise,则必须覆盖每个控制器并指定所有操作

      devise_for :users, :controllers => { :confirmations => "confirmations", :omniauth_callbacks => "omniauth_callbacks", :passwords => "passwords",  :registrations => "registrations", :sessions => "sessions", :unlocks => "unlocks" } do
      # etc
      end
      

      它适用于 Rails 3.0.x

      【讨论】:

        猜你喜欢
        • 2014-04-18
        • 2011-04-28
        • 2019-01-31
        • 1970-01-01
        • 2020-05-22
        • 1970-01-01
        • 2011-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多