Prefer using render plain

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: rails-best-practices/plain-text-rendering

Language: Ruby

Severity: Notice

Category: Best Practices

Description

This rule enforces the use of render plain: syntax in Ruby on Rails applications instead of the render text: syntax. This is because render text: defaults to rendering the MIME type as text/html, which may not always be the desired outcome. On the other hand, render plain: explicitly renders the MIME type as text/plain.

By using render plain:, the developer is being explicit about the MIME type that is being rendered, which can prevent potential issues down the line. It is especially crucial when working with text that should not be interpreted as HTML.

To adhere to this rule, simply replace any instances of render text: with render plain:. If the content type needs to be specified, it can be done so directly within the render plain: call, e.g., render plain: 'foo', content_type: 'text/plain'.

Non-Compliant Code Examples

# default MIME of `text/html`
render text: 'foo'

# can simplify with plain
render text: 'bar', content_type: 'text/plain'

Compliant Code Examples

render plain: 'foo'
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis