Partial template name without Ruby identifier Rails 5

Prajakta Tambe

By Prajakta Tambe

on 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.

2<!DOCTYPE html>
5  <%= render :partial => 'order-details' %>

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

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

Stay up to date with our blogs. Sign up for our newsletter.

We write about Ruby on Rails, ReactJS, React Native, remote work,open source, engineering & design.