让 Rails URL 助手自动输出 https url [英] Getting Rails URL helpers to automatically output https urls
问题描述
我正在开发一个大量混合 http 和 https 的网站 - 使链接使用正确的路由协议的最佳/最简单的方法是什么 - 可以在路由文件中指定吗?
I am developing a site that mixes http and https a lot - whats the best/easiest way to make the links use the right protocol for the route - can it be specified in the routes file?
假设我在 Rails 3 中有以下路线.
Say I have the following route in Rails 3.
match "/test" => "test#index", :as => :test, :constraints => { :protocol => 'https' }
如果我在一个 http 页面上,并且我使用 test_url()
,它将输出 http://domain.com/test.我想要 https://domain.com/test.
If I'm on a http page, and I use test_url()
, it'll output http://domain.com/test. I want https://domain.com/test instead.
我知道我可以使用 test_url(:secure => true)
,但这是重复的逻辑.
I know I can use test_url(:secure => true)
, but that's duplicating logic.
我知道我可以有 http://domain.com/test 到 https://domain.com/test,但这是一个额外的重定向,而且它在表单帖子上失败.
I know I could have http://domain.com/test to https://domain.com/test, but that's an extra redirect, plus it fails on form posts.
想法?
推荐答案
看起来这将在 Rails 4 中解决!https://github.com/rails/rails/commit/9b4514c3b8ecfbc40a44dbd4c2ebd4ce67f4a459
It looks like this will be solved in Rails 4! https://github.com/rails/rails/commit/9b4514c3b8ecfbc40a44dbd4c2ebd4ce67f4a459
这篇关于让 Rails URL 助手自动输出 https url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!