Rails URL 生成器切换 http/https
有没有办法放入外部资源的链接,根据当前协议自动添加协议?
例如,我想显示来自 Facebook Graph API 的图像。我希望我可以做类似的事情:
image_tag url_for("/1234567/picture", :host => "graph.facebook.com")
这样 url_for
只是将协议基于当前请求的协议。
我知道这可行,但我希望有更好的方法:
image_tag("#{request.protocol}://graph.facebook.com/1234567/picture")
Is there a way to put in links to external resources that automatically adds the protocol based on the current protocol?
For example I want to show images from Facebook's Graph API. I was hoping I could do something like:
image_tag url_for("/1234567/picture", :host => "graph.facebook.com")
So that the url_for
just bases the protocol on the current request's protocol.
I know this works but I'm hoping there's a better way:
image_tag("#{request.protocol}://graph.facebook.com/1234567/picture")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将协议选项添加到 url_for。
或者也许更好的方法是创建一个名为
url_for_same_protocol
(或任何你想要的)的助手(如果你经常这样做):..然后只需替换你的
url_for
使用url_for_same_protocol
调用。You can add the protocol option to url_for.
or maybe a better way would be to create a helper (if you do this a lot) called
url_for_same_protocol
(or whatever you want):.. and then just replace your
url_for
call withurl_for_same_protocol
.