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

发布于 2024-12-07 03:44:09 字数 934 浏览 1 评论 0原文

我想要混合 https/http 网站。

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

像以下这样的宝石:

  • rack-sslrack
  • -ssl-enforcer

可以完美工作,但前提是您想在 https 上拥有整个网站 “混合 http/https”在 A、B、C 操作中仅使用 ssl,而在 D、E、F 操作中仅使用 http - 不起作用。

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

Rails 3 SSL 路由重定向https 到 http

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

但是链接存在一个问题,例如:

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

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

问题

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

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

编辑:

我使用 Rails 3.0.9。

I want to have mixed https/http site.

Moreover I want have redirects from https to http(ie. after user login successfully it should redirect to root page at http).

Gems like:

  • rack-ssl
  • rack-ssl-enforcer

works perfectly but only If you want to have entire site at https
"Mixed http/https" with only ssl at A, B, C actions and only http at D, E, F - dont work.

I checked solution from another SO thread:

Rails 3 SSL routing redirects from https to http

Almost works.
Its easy to write script which will change(on entire views) helper from "_path" to "_url".

But there is a problem with links like:

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

There are many diffrent models and I use often "model" at iteration blocks, so solution based on 'rewrite' script will dont work with that.

Questions:

Is there a way to change behavior of <%= link_to 'model', model %> code to fix that? Is there a possibility to overwrite path helper(standard protocol will be http, on giver parameter - https)?

Or maybe there is a another solution which I have not found yet?

Edit:

I work with Rails 3.0.9.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-12-14 03:44:09

如果您想将 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]

If you would like to add https to a particular route

you can use this code

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

You can define the routes you would like to use with the before_filter action simply by doing the following

before_filter :redirect_to_https, :except => [:action1 , :action2]
before_filter :redirect_to_https, :only => [:action1 , :action2]
心的位置 2024-12-14 03:44:09

使用这个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

Use this gem:

https://github.com/retr0h/ssl_requirement

gem install ssl_requirement

Then to add ssl_required :new, :destroy #others actions
to your controllers.

If you use devise you have to overwrite each controller and specify all actions

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

It works with Rails 3.0.x

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文