Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from Feb 18, 2014

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
@@ -2,6 +2,8 @@ module ActionController
module Rendering
extend ActiveSupport::Concern

RENDER_FORMATS_IN_PRIORITY = [:body, :text, :plain, :html].freeze
Copy link
Member

@rafaelfranca rafaelfranca Feb 14, 2014

We don't need to freeze this

```

TIP: Rendering pure text is most useful when you're responding to Ajax or web service requests that are expecting something other than proper HTML.

NOTE: By default, if you use the `:text` option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the `layout: true` option.
NOTE: By default, if you use the `:plain` option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the `layout: true` option.
Copy link
Member

@robin850 robin850 Feb 15, 2014

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

Awesome work so far! ❤️

@dhh
Copy link
Member

@dhh 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 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 sikachu commented Feb 18, 2014

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

type, by using the `:body` option to `render`:

```
ruby render body: "raw"
Copy link
Member

@rafaelfranca rafaelfranca Feb 18, 2014

this "ruby" is not in the wrong line?

Copy link
Member Author

@sikachu sikachu Feb 18, 2014

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

sikachu added 8 commits Feb 18, 2014
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`.
sikachu added 2 commits Feb 18, 2014
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 issue 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 Feb 18, 2014
robertomiranda added a commit to robertomiranda/rails that referenced this issue Feb 18, 2014
tenderlove added a commit that referenced this issue 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
tenderlove
Copy link
Member

tenderlove commented on 3047376 Feb 28, 2014

Why are we mutating the header hash here? It didn't used to do that.

If you're using a streaming response, this will break because the header hash is frozen (since it has already been sent to the client).

Also, what if someone specifically set a content type in their response? Do we actually want to delete it? For example:

class MainController < ApplicationController
  def index
    response.headers['Content-Type'] = 'application/json'
    render body: JSON.dump([1,2,3,4])
  end
end

I know we have a render json thing, but this is just an example. Are we sure unconditionally rm'ing the content type header is The Right Thing™?

/cc @jeremy

jeremy
Copy link
Member

jeremy commented on 3047376 Mar 1, 2014

👍 we want to not set the default content type, but we don't want to erase the content type you provided—that's precisely why people would be using render body: ... 😁

sikachu
Copy link
Member

sikachu commented on 3047376 Mar 1, 2014

Yep, this patch is going too far. Going to revert this then 😅.

See #14238 for more detail.

BDJRA
Copy link

ghost commented on 3047376 Feb 28, 2014

homu added a commit to rubygems/rubygems.org that referenced this issue 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 issue 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
Linked issues

Successfully merging this pull request may close these issues.

7 participants