我想要混合 https/http 站点。
此外,我希望从 https 重定向到 http(即,在用户成功登录后,它应该重定向到 http 的根页面)。
gem 如:
- rack-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
几乎可以正常工作。 它很容易编写脚本,它将(在整个 View 上)助手从“_path”更改为“_url”。
但是像这样的链接有问题:
<%= link_to "model", some_model %>
<%= link_to "edit model", edit_mode_url(model) %>
...
有许多不同的模型,我经常在迭代 block 中使用“模型”,因此基于“重写”脚本的解决方案不适用于该模型。
问题:
有没有办法改变 <%= link_to 'model', model %>
的行为?代码来解决这个问题?是否有可能覆盖路径助手(标准协议(protocol)将是 http,在给定参数 - https 上)?
或者我还没有找到另一种解决方案?
编辑:
我使用 Rails 3.0.9。
最佳答案
如果你想将 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]
关于ruby-on-rails-3 - Rails 3,从 https 重定向到 https,覆盖 _path 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7584062/