Rails 3,从https重定向到https,覆盖_path助手

时间:2011-09-28 13:33:21

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

我想要混合使用https / http网站。

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

Gems喜欢:

  • rack-ssl
  • rack-ssl-enforcer

完美无缺,但仅限于如果您希望将整个网站设为https “混合的http / https”只有ssl在A,B,C动作,只有http在D,E,F - 不起作用。

我检查了另一个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) %>
...

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

问题:

有没有办法改变<%= link_to 'model', model %>代码的行为来解决这个问题?是否有可能覆盖路径助手(标准协议将是http,on giver参数 - https)?

或许还有另一个我还没有找到的解决方案?

修改

我使用Rails 3.0.9。

2 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

使用此gem:

https://github.com/retr0h/ssl_requirement

gem install ssl_requirement

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

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

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