ERB templates combine HTML with embedded Ruby, which makes dense views difficult to scan and easy to misread. This tool formats the surrounding markup while keeping ERB tags in place, or compacts a template by collapsing HTML whitespace and removing HTML comments.
The operation runs locally in your browser. It does not render the template, evaluate Ruby, contact a Rails application, or check whether referenced variables and helpers exist.
How to Use the ERB Beautifier
Format a template for review or produce a more compact copy.
- Choose Beautify or Minify in the Action control.
- When Beautify is selected, choose 2 spaces, 4 spaces, or Tab from Indent.
- Paste or type the template in Source code; Output updates after a short pause.
- Select Use output as input if you want to continue working from the generated result.
- Select Copy to copy the current Output.
How ERB and HTML Formatting Work Together
ERB, or Embedded Ruby, places Ruby expressions and statements inside a text document. Rails commonly uses it for HTML views. Output tags such as <%= user.name %> insert a value, while control tags such as <% if user.admin? %> run Ruby without directly inserting their result.
<% if @orders.any? %>
<p><%= @orders.length %> orders</p>
<% end %>
In Beautify mode, this tool uses the HTML formatter from js-beautify with ERB templating enabled. Before formatting, it places non-output control tags—the <% ... %> form, but not <%= ... %>—on separate lines. That gives the formatter clearer boundaries for indenting the HTML around conditionals and loops.
This is layout transformation, not Ruby-aware refactoring. The tool does not parse the Ruby inside a tag, rename variables, normalize method calls, or prove that blocks are balanced. Review complex templates after formatting, especially when a tag contains multiline Ruby or generates markup indirectly.
ERB Tags, Whitespace, and Common Formatting Mistakes
ERB has several tag forms with different effects. <%= ... %> emits an expression result, <% ... %> executes code, and <%# ... %> is an ERB comment. Ruby and Rails can also use trimming markers such as <%- or -%> to influence nearby whitespace. These meanings come from ERB processing, not from HTML indentation.
A frequent mistake is treating a template as plain HTML and moving ERB tags without considering the generated output. Whitespace can be visible between inline elements, inside preformatted content, or in email templates. Formatting may also expose awkward structure without correcting it: an opening HTML element in one conditional branch and its closing element in another remains fragile.
Keep presentation markup and control flow reasonably shallow. If a view contains long calculations or deeply nested branching, moving that work to a helper, presenter, component, or model-facing layer can make the template easier to test. A beautifier can reveal nesting, but it cannot resolve an overloaded view architecture.
For markup-only work, the HTML beautifier avoids ERB-specific handling. To convert static HTML syntax for a React component, use the HTML to JSX converter, but do not expect it to translate embedded Ruby.
What ERB Minification Changes and Leaves Alone
Minify mode uses html-minifier-terser with whitespace collapsing and HTML comment removal enabled. Its default custom-fragment handling leaves ERB fragments such as <% ... %> intact. The Indent control is disabled in this mode because indentation is not part of the compact output.
Minification does not compile Rails views or compress the Ruby within ERB tags. It does not remove ERB comments as a dedicated Ruby-aware operation, optimize helper calls, or verify that the result renders the same DOM. The reported byte comparison measures the UTF-8 size of the source and output strings in the browser; it is not a measurement of a compressed HTTP response.
Whitespace collapsing can change rendered text where spaces are meaningful. HTML comments may also be used by build systems, email workflows, or legacy conditional mechanisms. Test the rendered template in its real Rails context before replacing a maintained source file with minified output. Keeping readable ERB in source control and relying on deployment compression is often the more maintainable design.
Privacy and Safe Handling of Rails Templates
Formatting and minification happen in the browser, and the tool makes no request containing the template. That reduces exposure to a formatting service, but the source may still contain information that should not be shared, such as internal route names, feature flags, customer examples, or accidentally pasted secrets.
The formatter does not execute Ruby, so code inside ERB tags does not run here. It also does not render generated HTML. Those boundaries make inspection safer, but they do not establish that the template itself is secure.
An output expression can still create cross-site scripting risk when a Rails application renders unsafe content or deliberately bypasses escaping. Likewise, formatting cannot detect authorization mistakes, unsafe URL construction, or sensitive values embedded in comments. Review data flow and escaping in the application, and use sanitized sample content when asking others to inspect a template.
Frequently Asked Questions
Does the ERB beautifier execute Ruby code?
No. It treats ERB tags as template fragments while js-beautify formats the surrounding HTML. It does not start Ruby, load Rails, call helpers, or render the template, so it cannot show the final page or catch runtime exceptions.
Will beautifying ERB change the generated HTML?
It can change whitespace around tags and elements, which may affect visible spacing in whitespace-sensitive layouts. The Ruby text inside an ERB fragment is not reformatted, but control tags may be moved onto their own lines. Compare rendered output in the application before adopting a generated version.
What does Minify remove from an ERB template?
It asks html-minifier-terser to collapse HTML whitespace and remove HTML comments. ERB fragments are left as custom fragments rather than executed or compressed as Ruby. Because whitespace and comments can carry meaning in specialized templates, inspect and test the result.
Does this tool validate ERB or Rails syntax?
No. A formatted result does not prove that Ruby syntax is valid, blocks are balanced, helpers exist, or HTML generated at runtime is well formed. Use Rails tests, template rendering, and Ruby-aware linting for those checks.
Is it safe to paste a private Rails view here?
The transformation runs locally in the browser and the code does not upload the template. Still, remove credentials, personal data, and production-only details before using any general-purpose utility. The tool formats text; it does not classify or redact sensitive content.
References: Ruby documentation: ERB · js-beautify documentation.