如何针对某些路径在Rails 4中有选择地启用SSL?

如何为给定路径关闭SSL HTTPS? 我看到为某些资源操作启用SSL,但这是为单个路径启用HTTPS。 我的配置有

config.force_ssl = true 

但是,当我显示一个包含iframe的页面并嵌入外部源时

 <iframe src='http://www. 

然后它甚至没有出现(Chrome),因为它是混合内容。 所以我想为具有iframe的单一路由禁用SSL。 (我真的想根据内容打开和关闭它。)

我尝试将其添加到我的routes.rb

 get 'frame', :constraints => { :protocol => 'http' } 

但是这给出了一个错误http://127.0.0.1:3000/posts/136/frame

 No route matches [GET] "/posts/136/frame" 

即使路线出现了

 frame_post GET /posts/:id/frame(.:format) posts#frame {:protocol=>"http"} 

我还看到了Rails 3.1中特定路由的强制SSL并尝试过

 gem 'rack-ssl-enforcer' # Gemfile config.middleware.use Rack::SslEnforcer, :except => [ /\/frame$/ ], :strict => true # application.rb 

但它仍然总是重定向到https。 我也试过了

 force_ssl :except => :frame # posts_controller.rb 

但这也不起作用。

我正在使用Rails 4.1。

Rack ssl enforcer是一个很好的解决方案 – 你的正则表达式是错误的。 在您的表单中,正则表达式只会排除完全为“/ frame”的路径,并且不会排除在/ frame之前有任何内容的路径

如果要排除 / frame 结尾的任何路径,正确的正则表达式将是:

 \.*\/frame$/ 

导致配置:

 config.middleware.use Rack::SslEnforcer, :except => [ \.*\/frame$/ ], :strict => true