Skip to content

Commit

Permalink
Update guides for new rendering options
Browse files Browse the repository at this point in the history
* Introduces `:plain`, `:html`, `:body` render option.
* Update guide to use `render :plain` instead of `render :text`.
  • Loading branch information
sikachu committed Feb 18, 2014
1 parent 79c4983 commit 76be30f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion guides/source/action_controller_overview.md
Expand Up @@ -1088,7 +1088,7 @@ class ApplicationController < ActionController::Base
private

def record_not_found
render text: "404 Not Found", status: 404
render plain: "404 Not Found", status: 404
end
end
```
Expand Down
2 changes: 1 addition & 1 deletion guides/source/getting_started.md
Expand Up @@ -608,7 +608,7 @@ look like, change the `create` action to this:

```ruby
def create
render text: params[:article].inspect
render plain: params[:article].inspect
end
```

Expand Down
40 changes: 36 additions & 4 deletions guides/source/layouts_and_rendering.md
Expand Up @@ -236,15 +236,34 @@ render inline: "xml.p {'Horrid coding practice!'}", type: :builder

#### Rendering Text

You can send plain text - with no markup at all - back to the browser by using the `:text` option to `render`:
You can send plain text - with no markup at all - back to the browser by using
the `:plain` option to `render`:

```ruby
render text: "OK"
render plain: "OK"
```

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.
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.

#### Rendering HTML

You can send a HTML string back to the browser by using the `:html` option to
`render`:

```ruby
render html: "<strong>Not Found</strong>".html_safe
```

TIP: This is useful when you're rendering a small snippet of HTML code.
However, you might want to consider moving it to a template file if the markup
is complex.

NOTE: This option will escape HTML entities if the string is not html safe.

#### Rendering JSON

Expand Down Expand Up @@ -276,6 +295,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"
```

TIP: This option should be used only if you explicitly want the content type to
be unset. Using `:plain` or `:html` might be more appropriate in most of the
time.

#### Options for `render`

Calls to the `render` method generally accept four options:
Expand Down

0 comments on commit 76be30f

Please sign in to comment.