Skip to content

Instantly share code, notes, and snippets.

@leikind
Created April 20, 2014 11:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leikind/11112215 to your computer and use it in GitHub Desktop.
Save leikind/11112215 to your computer and use it in GitHub Desktop.
rails render and content type
render :inline => '<html><b>123</b></html>' #=> Content-Type: text/html; charset=utf-8
# Good
render json: {1 => 2}.to_json #=> Content-Type: application/json; charset=utf-8
# this was correct
render xml: '<xml><foo>123</foo></xml>' #=> Content-Type: application/xml; charset=utf-8
# correct, too
render text: 'hello' #=> Content-Type: text/html; charset=utf-8
# What? text/html? Why?
# Forcing text/plain :
render text: 'hello', content_type: 'text/plain' #=> Content-Type: text/plain; charset=utf-8
# Important! Render inside a format block overrides content_type:
respond_to do |format|
format.js do
render 'projects/new', layout: nil #=> Content-Type: text/javascript; charset=utf-8
end
end
# This is actually logical
# But if you need to override it:
respond_to do |format|
format.js do
render 'projects/new', layout: nil, content_type: 'text/html' #=> Content-Type: text/html; charset=utf-8
end
end
@rodjosh
Copy link

rodjosh commented Feb 14, 2023

Thanks, saved me !!

@leikind
Copy link
Author

leikind commented Feb 15, 2023

Thanks, saved me !!

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment