获取Rails URL帮助程序以自动输出https URL
我正在开发一个混合了http和https的网站 – 最简单/最简单的方法是让链接使用正确的路由协议 – 是否可以在路由文件中指定?
假设我在Rails 3中有以下路由。
匹配“/ test”=>“test #index”,:as =>:test,:constraints => {:protocol =>'https'}
如果我在http页面上,并且我使用test_url()
,它将输出http://domain.com/test 。 我想改为https://domain.com/test 。
我知道我可以使用test_url(:secure => true)
,但这是重复的逻辑。
我知道我可以http://domain.com/test到https://domain.com/test ,但这是一个额外的重定向,加上它在表单post上失败了。
想法?
将test_url(:protocol => 'https')
用于httpsurl。
还没试过,但在ApplicationController
添加:
def default_url_options(options={}) { :secure => true } end
def default_url_options(options={}) { :protocol => "https" } end
对于Rails 3.2,我使用@ apneadiving的答案组合。 将以下代码添加到我的ApplicationController
def default_url_options(options={}) options.merge{ :protocol => "https" } end
Rails 3 SSL路由重定向从https到http很好地回答了这个问题。 简而言之,这并不是一个好方法。 我提交了以下Rails错误: https : //github.com/rails/rails/issues/3571
看起来这将在Rails 4中得到解决! https://github.com/rails/rails/commit/9b4514c3b8ecfbc40a44dbd4c2ebd4ce67f4a459
你可以使用一个名为ss_requirement的插件,它会为你提供像ssl_required ssl_allowed这样的方法
您只需在控制器中添加此项即可在任何操作上启用disable禁用https
ssl_allowed :login, :update_profile