如何在 ruby​​ 中发送原始标头

发布于 2024-07-25 07:38:24 字数 417 浏览 0 评论 0原文

我有一个使用 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 技术交流群。

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

发布评论

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

评论(3

四叶草在未来唯美盛开 2024-08-01 07:38:24

您正在使用 Ruby on Rails 吗?

request.headers["Content-Type"] # => "text/plain"

或者也许是 Ruby 的 CGI 库?

http://www.ruby-doc。 org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000098

Are you using Ruby on Rails?

request.headers["Content-Type"] # => "text/plain"

Or maybe Ruby's CGI library?

http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000098

ぽ尐不点ル 2024-08-01 07:38:24

如果您使用 Rails,您需要:

response.content_type = Mime::HTML
response.charset      = "utf-8"

您还可以尝试直接设置标头:

response.headers["Content-Type"] = "text/html; charset=utf-8"

如果您使用 Rack,您希望使用元组的第二个元素设置标头:

class MyRackApp
  def call(env)
    response = []
    # do stuff with env, populating response
    # response is [status_code, headers, body]
    response[1]["Content-Type"] = "text/html; charset=utf-8"
    response
  end
end

如果您使用原始 CGI(我肯定会推荐 Rack 而不是 cgi.rb):

header("text/html; charset=utf-8")

If you're using Rails, you want:

response.content_type = Mime::HTML
response.charset      = "utf-8"

You could also try to set the headers directly:

response.headers["Content-Type"] = "text/html; charset=utf-8"

If you're using Rack, you want to set the header using the second element of the tuple:

class MyRackApp
  def call(env)
    response = []
    # do stuff with env, populating response
    # response is [status_code, headers, body]
    response[1]["Content-Type"] = "text/html; charset=utf-8"
    response
  end
end

If you're using raw CGI (I would definitely recommend Rack over cgi.rb):

header("text/html; charset=utf-8")
゛时过境迁 2024-08-01 07:38:24

我不确定如何在不了解有关如何生成页面的更多信息的情况下直接回答这个问题,但我可能建议您研究一些用于 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'

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