Rails 表单导致混合内容警告

Rails forms leading to Mixed Content warning

在我的 Rails 应用程序中,我同时使用 Rails' 原生 form_tag 方法和 simple_form ruby​​gem 提供的 simple_form_for 方法。

它们都在 chrome 控制台中导致以下警告:

Mixed Content: The page at 'https://example.com' was loaded over a secure connection, but contains a form which targets an insecure endpoint 'http://example.com'. This endpoint should be made available over a secure connection.

事实上,呈现的 HTML 表单使用 http 协议作为其 action 属性。

这是什么原因?我所有其他 URL 都使用 https 协议。

您为您的环境设置了不正确的协议

您的服务器 运行 正在 HTTPS 上并且表单创建 HTTP URL。

您需要告诉您的 URL 助手 使用与您的服务器 运行ning 相同的协议构建 URLs你的环境。

开发 应该 运行 在 HTTP 上,staging 应该是 HTTPS 并且 production 还有 HTTPS。

有多种方法可以做到这一点。最好是在您的环境 配置文件 中设置协议。所以放置这一行:

Rails.application.routes.default_url_options[:protocol] = 'https'

进入您的环境配置文件,例如 production.rbstaging.rb.

另一种方法是根据控制器操作设置默认协议。 Check this one for more info.

如果您使用的是 mailer,还要检查您的邮件协议设置。如所述 here.