Skip to content

Introduce :plain, :html, and :body render options.#14062

Merged
rafaelfranca merged 10 commits intorails:masterfrom
sikachu:ps-render-format
Feb 18, 2014
Merged

Introduce :plain, :html, and :body render options.#14062
rafaelfranca merged 10 commits intorails:masterfrom
sikachu:ps-render-format

Conversation

@sikachu
Copy link
Member

@sikachu sikachu commented Feb 14, 2014

This is a continuation from #12374, with slight modification since the discussion went a bit off-topic.


Per discussion, render :text misdirect people to think that it would render content with text/plain MIME type. However, render :text actually sets the response body directly, and inherits the default response MIME type, which is text/html.

In order to reduce confusion, we're introducing 3 more render format to render:

render html: '<strong>HTML String</strong>'.html_safe # render with `text/html` MIME type.

render plain: 'plain text' # render with `text/plain` MIME type.

render body: 'raw body' # render raw content, does not set content type.

We want to phrase out the usage of render :text, to reduce the confusion in the future. There were some discussion about deprecate render :text, but we haven't come into a conclusion yet. So, we'll consider doing that before the next major release.

Fixes #12374

@sikachu sikachu added this to the 4.1.0 milestone Feb 14, 2014
@sikachu sikachu self-assigned this Feb 14, 2014
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to freeze this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you wrap new additions in the guides around 80 chars please ?

Awesome work so far! ❤️

@dhh
Copy link
Member

dhh commented Feb 18, 2014

@sikachu, do you expect to be able to wrap this up today or should we delay it to 4.2.0?

@sikachu
Copy link
Member Author

sikachu commented Feb 18, 2014

I can respond to reviews this morning! Sorry for the delay.

On Tue, Feb 18, 2014 at 5:10 AM, David Heinemeier Hansson
notifications@github.com wrote:

@sikachu, do you expect to be able to wrap this up today or should we delay it to 4.2.0?

Reply to this email directly or view it on GitHub:
#14062 (comment)

@sikachu
Copy link
Member Author

sikachu commented Feb 18, 2014

I've addressed all of the comments so far. This should be ready to go.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this "ruby" is not in the wrong line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. Fixing ...

On Tue, Feb 18, 2014 at 12:02 PM, Rafael Mendonça França
notifications@github.com wrote:

@@ -276,6 +298,19 @@ render js: "alert('Hello Rails');"

This will send the supplied string to the browser with a MIME type of text/javascript.

+#### Rendering raw body
+
+You can send a raw content back to the browser, without setting any content
+type, by using the :body option to render:
+
+```
+ruby render body: "raw"

this "ruby" is not in the wrong line?

Reply to this email directly or view it on GitHub:
https://github.com/rails/rails/pull/14062/files#r9831304

This is an option for sending a raw content back to browser. Note that
this rendering option will unset the default content type and does not
include "Content-Type" header back in the response.

You should only use this option if you are expecting the "Content-Type"
header to not be set. More information on "Content-Type" header can be
found on RFC 2616, section 7.2.1.

Please see rails#12374 for more detail.
This is as an option to render content with a content type of
`text/plain`. This is the preferred option if you are planning to render
a plain text content.

Please see rails#12374 for more detail.
This is an option for to HTML content with a content type of
`text/html`. This rendering option calls `ERB::Util.html_escape`
internally to escape unsafe HTML string, so you will have to mark your
string as html safe if you have any HTML tag in it.

Please see rails#12374 for more detail.
This test were assuming that the list of render options will always be
the same. Fixing that so this doesn't break when we add/remove render
option in the future.
* Introduces `:plain`, `:html`, `:body` render option.
* Update guide to use `render :plain` instead of `render :text`.
Setting this attribute to `true` will remove the content type header
from the request. This is use in `render :body` feature.
rafaelfranca added a commit that referenced this pull request Feb 18, 2014
Introduce `:plain`, `:html`, and `:body` render options.
@rafaelfranca rafaelfranca merged commit acc0e63 into rails:master Feb 18, 2014
@sikachu sikachu deleted the ps-render-format branch February 18, 2014 17:14
robertomiranda added a commit to robertomiranda/rails that referenced this pull request Feb 18, 2014
tenderlove added a commit that referenced this pull request Feb 20, 2014
* master: (1455 commits)
  change 'assert !' to 'assert_not' in guides [ci skip]
  Pointing to latest guides [ci skip]
  Methods silence_stream/quietly are not thread-safe [skip ci]
  [ci skip] Close the meta tag with '/>' instead of '>'
  Fix render plain docs example in AM::Base
  Update Docs in favor to use render plain instead of text option ref #14062
  Typo fix for unscope
  Use the reference for the mime type to get the format
  Preparing for 4.1.0.beta2 release
  Correctly escape PostgreSQL arrays.
  Escape format, negative_format and units options of number helpers
  Sync 4.1 release notes with changes since 7f648bc [ci skip]
  Update upgrading guide regarding `render :text`
  Add `#no_content_type` attribute to `AD::Response`
  Add missing CHANGELOG entry to Action View
  Update guides for new rendering options
  Cleanup `ActionController::Rendering`
  Fix a fragile test on `action_view/render`
  Introduce `render :html` for render HTML string
  Introduce `render :plain` for render plain text
  ...

Conflicts:
	actionmailer/lib/action_mailer/railtie.rb
	railties/lib/rails/application.rb
	railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt
	railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt
homu added a commit to rubygems/rubygems.org that referenced this pull request Dec 26, 2016
Use render plain: instead of text:

This is in preparation of rails 5 update. Chips away: e0a5d2e ( minus env deprecation).

`render plain: ` was introduced in rails 4.1. rails/rails#14062
Edouard-chin added a commit to Shopify/response_bank that referenced this pull request Jul 19, 2017
- `render plain:` was introduced in Rails 4.1 rails/rails#14062
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce render :plain and render :html, make render :body as an alias to render :text

7 participants