Directory for shared partials in Rails

Which directory should contain shared partials in a Rails app?

Rejected option: app/views/layouts directory

I’ve seen this:

app/views/layouts/application.html.erb
app/views/layouts/_flashes.html.erb
app/views/layouts/_footer.html.erb
app/views/layouts/_header.html.erb
app/views/layouts/_javascript.html.erb
app/views/layouts/_navigation.html.erb

However, the layout directory should contain… layouts.

app/views/layouts/application.html.erb

Rejected option: app/views/shared directory

I’ve seen this:

app/views/shared/_flashes.html.erb
app/views/shared/_footer.html.erb
app/views/shared/_header.html.erb
app/views/shared/_javascript.html.erb
app/views/shared/_navigation.html.erb

That was the recommendation in the classic, Agile Web Development with Rails.

Also, when you extracted a partial from a layout in rails.vim using :Rextract, it would automatically place the extracted partial into the shared directory.

Current solution: app/views/application directory

This is our current standard:

app/views/application/_flashes.html.erb
app/views/application/_footer.html.erb
app/views/application/_header.html.erb
app/views/application/_javascript.html.erb
app/views/application/_navigation.html.erb

The main reason is a subtle, but functional difference: template inheritance.

Also, invoking the partials is slightly shorter with this approach. Instead of:

render 'shared/navigation'

This is possible from every view:

render 'navigation'

We have formalized this convention in Suspenders

Sharing is caring.

Care bears

Pair with one of our expert developers to level up your skills with Coaching by thoughtbot. Save time learning best practices and techniques for reducing technical debt in Ember, Ruby, Haskell, and Go in 1-on-1 sessions tailored to your goals.