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`rendertext:'foo'# can simplify with plainrendertext:'bar',content_type:'text/plain'
Compliant Code Examples
renderplain:'foo'
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- rails-best-practices # Rules to enforce Ruby best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines