Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 817
user3002315 Avatar asked Oct 23 '25 19:10

user3002315


1 Answers

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:

like image 50
Victor Avatar answered Oct 25 '25 10:10

Victor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!