Created
April 20, 2014 11:49
-
-
Save leikind/11112215 to your computer and use it in GitHub Desktop.
rails render and content type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, saved me !!