Rails adds support for annotating HTML output with template file names.

As a developer,
we have all come across such web pages where it is difficult to figure out which template(s) are being used to render the page.

Before Rails 6.1

We have gems like xray-rails which can be used to find out which templates are used to render a page. However, Rails itself didn’t provide any option till now which we could use.

With Rails 6.1

Rails 6.1 has now added support for it. It now provides us with a new config called annotate_rendered_view_with_filenames for the same.

config.action_view.annotate_rendered_view_with_filenames = true

When set to true, it will render template filenames as comments in HTML.

HTML output will look something like this-

Limitations

Currently, this only adds support for erb templates.

When using HAML, SLIM, etc templates, HTML output will look like this.

In the above example, we have used HAML templates for all files except for application.html.erb. Here we can see that the name of application.html.erb file is shown as a comment, but there are no comments for other files.

Summary

This type of comments help us in faster debugging and locating of content in rendered files.

Need help on your Ruby on Rails or React project?

Join Our Newsletter