Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
headers['content-type'] vs. response.content-type
3 Posts
headers['content-type'] vs. response.content-type

When I want to set the HTTP response's content MIME type, should I use headers["content-type"]="text/html";

or response.content_type = "text/html"

What is the difference between the two? (I think there is no difference, just need confirmation.)

Which one is preferred usage in RoR?

You should use

response.content_type = Mime::HTML

The difference is that the former set the headers field directly. The latter is a method that also supports setting the charset at the same time if required.

Check the documentation here: http://api.rubyonrails.org/classes/ActionController/Response.html#M000593

Thank you. That helps.

3 Posts
Login to add your message