如何在 ruby 中发送原始标头
我有一个使用 UTF-8 字符集的页面,但是页面上的字符被破坏了,我认为这只是设置标题“Content-Type: text/html; charset=utf-8”的问题。我知道如何在 PHP 中执行此操作,只需将以下内容放在页面顶部即可。
<?php header("Content-Type: text/html; charset=utf-8"); ?>
有没有办法在红宝石中做到这一点? 你能像这样在页面顶部放置一个标题吗?
update: Jun 29, 1:20p PST
我不使用它作为 Rails 应用程序的一部分。 它用于独立应用程序中的嵌入式浏览器页面,我可以使用 Javascript 和/或 Ruby 创建动态页面。
I have a page which uses the UTF-8 character set, however the characters are mangled on the page, I think this is just a matter of setting a header "Content-Type: text/html; charset=utf-8" ... I know how to do this in PHP, simply place the following at the top of the page.
<?php header("Content-Type: text/html; charset=utf-8"); ?>
Is there a way to do this in ruby? Can you place a header at the top of a page, like that?
update: Jun 29, 1:20p PST
I'm not using this as part of a rails application. It is for an embedded browser page in a stand-alone application, I can use Javascript and/or Ruby to create dynamic pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用 Ruby on Rails 吗?
或者也许是 Ruby 的 CGI 库?
http://www.ruby-doc。 org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000098
Are you using Ruby on Rails?
Or maybe Ruby's CGI library?
http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000098
如果您使用 Rails,您需要:
您还可以尝试直接设置标头:
如果您使用 Rack,您希望使用元组的第二个元素设置标头:
如果您使用原始 CGI(我肯定会推荐 Rack 而不是 cgi.rb):
If you're using Rails, you want:
You could also try to set the headers directly:
If you're using Rack, you want to set the header using the second element of the tuple:
If you're using raw CGI (I would definitely recommend Rack over cgi.rb):
我不确定如何在不了解有关如何生成页面的更多信息的情况下直接回答这个问题,但我可能建议您研究一些用于 Ruby 的轻量级非 Rails Web 框架。 有很多,它们使事情变得如此简单。
例如,Rack 有一个易于使用的哈希值用于发送到浏览器的标头。 同样,在 Camping 中,您可以执行类似
@headers['Content-类型'] = '文本/css'
I'm not sure how to answer this directly without learning more about how you're generating the page, but I might suggest you look into some of the lightweight non-Rails web frameworks for Ruby. There are many and they make things like this easy.
For example, Rack has an easy-to-use hash for Headers to send to the browser. Likewise in Camping you can just do something like
@headers['Content-Type'] = 'text/css'