我正在开发一个混合了http和https的网站 - 最简单/最简单的方法是让链接使用正确的路由协议 - 是否可以在路由文件中指定?
假设我在Rails 3中有以下路由.
match "/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,但这是一个额外的重定向,加上它在表单帖子上失败了.
想法?
我正在将应用程序升级到Rails 3.0.0,并且想知道添加SSL的标准方法是否已经改变(我依旧记得演示指示路由器现在可以处理SSL,但我不确定它是否仅用于演示目的).我目前使用"ssl_requirement"gem,但它给出:
弃用警告:不推荐使用#request_uri.请改用完整路径.(来自/Library/Ruby/Gems/1.8/gems/ssl_requirement-0.1.0/lib/ssl_requirement.rb:53上的ensure_proper_protocol)
此外,它在处理新的"数据方法"属性时似乎会中断.例如:
<%= link_to "Logout", user_path, :method => :delete %>
Run Code Online (Sandbox Code Playgroud)
从应用程序的SSL部分访问时工作正常,但从非SSL部分跟随时失败(尝试呈现显示操作)(用户控制器中的所有操作都需要SSL,但我知道destroy操作不会传输安全数据).
我想利用rails 3.1rc4 中的force_ssl
功能.
class ApplicationController < ActionController::Base
force_ssl
end
Run Code Online (Sandbox Code Playgroud)
问题是这打破了我现有的RSpec控制器规格的大部分/全部.例如,这失败了:
describe "GET 'index'" do
it "renders the Start page" do
get :index
response.should render_template 'index'
end
end
Run Code Online (Sandbox Code Playgroud)
而不是呈现页面的响应是301重定向到https://test.host/
.
如何更改我的规范以模拟HTTPS GET/POST?
我是否必须手动更改每个测试或是否有更简单的方法?(我意识到我force_ssl
只能在生产中调用,但这并不理想.真的,我应该测试force_ssl
确实会在预期时重定向到https://.)
相关:Rails 3 SSL路由重定向从https到http(遗憾的是无效).
重复,但答案对我不起作用:在设计上设置ssl
我有一个网络应用程序,现在已经工作了一段时间,但我需要将SSL添加到登录/编辑acct路径.我正在使用Devise进行身份验证.我在设计维基中找到了一个条目,使得这个过程看起来非常简单,但该死的,如果我可以让它工作的话.简单的部分是这样的:
#in config/environments/production.rb
config.to_prepare { Devise::SessionsController.force_ssl }
config.to_prepare { Devise::RegistrationsController.force_ssl }
Run Code Online (Sandbox Code Playgroud)
然后在这个要点中有大约25行代码:https://gist.github.com/1040964
我得到了足够好的工作,但是当我退出时,我从会话DELETE操作中获得了301,这使我获得了GET.
Started DELETE "/users/sign_out" for 98.246.164.160 at 2012-03-02 01:45:42 +0000
[02 Mar 01:45 10886 INFO] Processing by Devise::SessionsController#destroy as HTML
[02 Mar 01:45 10886 INFO] Parameters: {"authenticity_token"=>"fI4VZ4V0Go2Civo3sJz8Dv5/Wtaa90ynaYr+xxx="}
[02 Mar 01:45 10886 DEBUG] Parameters: {"_method"=>"delete", "authenticity_token"=>"fI4VZ4V0Go2Civo3sJz8Dv5/Wtaa90ynaYr+xxxx=", "action"=>"destroy", "controller"=>"devise/sessions"}
[02 Mar 01:45 10886 INFO] Redirected to https://ec2-xx-xx-106-255.us-west-2.compute.amazonaws.com/users/sign_out
[02 Mar 01:45 10886 INFO] Completed 301 Moved Permanently in 3ms
Started GET …
Run Code Online (Sandbox Code Playgroud)