让 Rails url 助手使用适当的协议?

发布于 2024-10-28 10:58:20 字数 470 浏览 2 评论 0原文

嘿,我允许通过 https 和 http 访问我的应用程序,除了使用任何 _url 方法时,一切都工作正常。如果我通过 https 访问使用此类方法的视图,它确实会作为 https 提供服务,但生成的 url 使用 http 协议。我想知道这是否正常,或者是否有办法使其自动。我希望 Rails 能够根据页面的访问方式自动生成适当的 url。

如果没有办法解决这个问题,那么创建适当的 url 的最佳方法是什么?这行得通吗?

if request.ssl?
  some_url(:protocol => "https")
else
  some_url
end

如果我能想出一种更自动化的方法,我会更喜欢它。也许如果 Rails url 方法生成 ://somedomain.com/some/path,这样我想它会自动适应正确的协议。

谢谢,我将不胜感激任何帮助。

Hey, I'm allowing my app to be accessed via https and http, and everything is working fine except for when I use any of the _url methods. If I access a view that uses such a method via https, it does get served as https, but the generated url uses the http protocol. I'm wondering if this is normal, or if there is a way to make it automatic. I was hoping rails would automatically generate the appropriate url based on how the page was accessed.

If there's no way around this, what would be the best way to create the appropriate url? Would this work?

if request.ssl?
  some_url(:protocol => "https")
else
  some_url
end

I would prefer it if I could come up with a more automatic approach. Maybe if the rails url methods generated ://somedomain.com/some/path, that way I imagine it would automatically adapt the correct protocol.

Thanks, I would appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-11-04 10:58:20

看起来有几种方法。在您的特定环境中,这些都可能不是理想的,但在 ActionView::Helpers< /a>,有一个 url_for 方法,如果您开始使用它,可能会得到您想要的东西。另一个简单的选项是使用相对 URL(即:“/controller/action”而不是“https://myawesomesite.com/controller/action”),它也可以由 url_for 方法生成。

除此之外,看起来您正在着手解决问题,或者深入研究 Rails 路径,生成代码并自行更改它。

这可能不是您正在寻找的最理想的答案,但这是我的 0.02 美元!

It looks like there are a few approaches. None of which may be ideal in your particular environment, but in the ActionView::Helpers, there is a url_for method that might get you what you want if you start using that. The other simple option is to use relative URLs (ie: '/controller/action' instead of 'https://myawesomesite.com/controller/action') which can be generated by the url_for method too.

Beyond that, it looks like you're getting in to work arounds, or digging in to the rails path generating code and altering it yourself.

Not probably the most ideal answer you were looking for, but there's my $0.02!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文