Partial template name without Ruby identifier Rails 5

Prajakta Tambe

Prajakta Tambe

July 14, 2016

This blog is part of our  Rails 5 series.

Before Rails 5, partials name should start with underscore and should be followed by any combination of letters, numbers and underscores.

This rule was required because before commit, rendering a partial without giving :object or :collection used to generate a local variable with the partial name by default and a variable name in ruby can't have dash and other things like that.

In the following case we have a file named _order-details.html.erb. Now let's try to use this partial.


<!DOCTYPE html>
<html>
<body>
  <%= render :partial => 'order-details' %>
</body>
</html>

We will get following error, if we try to render above view in Rails 4.x.

ActionView::Template::Error (The partial name (order-details) is not a valid Ruby identifier;
make sure your partial name starts with underscore,
and is followed by any combination of letters, numbers and underscores.):
2: <html>
3: <body>
4: Following code is rendered through partial named \_order-details.erb
5: <%= render :partial => 'order-details' %>
6: </body>
7: </html>

In the above the code failed because the partial name has a dash which is not a valid ruby variable name.

In Rails 5, we can give our partials any name which starts with underscore.

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.