user3002315
user3002315

Reputation: 91

Why do partials in Rails start with an underscore?

I'm not sure if this is convention or not, but I have been working through Hartl's Rails tutorial and I noticed that he creates all his partial files with a leading underscore. For example _user.html.erb. Is this something that is necessary for the partial to work, or is this a stylistic choice?

Upvotes: 4

Views: 3236

Answers (1)

Victor
Victor

Reputation: 638

It's necessary for partials to work. From the official Rails guide:

3.4.1 Naming Partials

To render a partial as part of a view, you use the render method within the view:

<%= render "menu" %>

This will render a file named _menu.html.erb at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:

Upvotes: 5

Related Questions