How to send the original ruby ​​headers

I have a page that uses the UTF-8 character set, however the characters on the page are garbled, I think it's just a matter of setting the heading "Content-Type: text / html; charset = utf-8" ... I know how to do this in PHP, just place the following at the top of the page.

<?php header("Content-Type: text/html; charset=utf-8"); ?>

Is there any way to do this in ruby? Can you place a heading at the top of the page, for example?


update: June 29, 1: 20p PST

I am not using this as part of a rails application. This is for the embedded browser page in a separate application, I can use Javascript and / or Ruby to create dynamic pages.

+3
source share
3 answers

If you use Rails, you want:

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

You can also try setting the headers directly:

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

If you use 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 are using the original CGI (I would definitely recommend Rack over cgi.rb):

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

Do you use Ruby on Rails?

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

Or maybe the Ruby CGI library?

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

+2
source

, , , , - Ruby. , .

, Rack . Camping - @headers['Content-Type'] = 'text/css'

0

Source: https://habr.com/ru/post/1711514/


All Articles