Alex Rivera | Logout

Where to put partials shared by the whole application in Rails?

Asked 2010-03-05T04:48:59.743
58

Where would I go about placing partial files shared by more than one model? I have a page called crop.html.erb that is used for one model - Photo. Now I would like to use it for another model called User as well.

I could copy and paste the code but that's not very DRY, so I figured I would move it into a partial.

Since it's shared between two models - where would I place that partial?

Thanks!

Edit
Report

1 Answer

57

Update

Layout inheritance is now in the guides under layout and rendering Template inheritance works similarly.

Rails 3.1 and following versions implement template inheritance, so I think the correct place for shared partials is now /app/views/application/, say you are in products#index you can do the following:

-# products#index
= render @products.presence || 'empty'

-# /app/views/application/_empty.html.haml
There are no items

btw it's application because the connection is the controller inheritance, so this assumes ProductsController < ApplicationController

This way if you implement /app/views/products/_empty.html.haml that will be taken, the above is a fallback for all the missing partials, and I can't check right now, but I think even for the template itself...

Railscast: template inheritance!

answered 2012-03-27T14:56:44.470

Your Answer