Prefer using render plain

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

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