44

I am using Ruby on Rails 3.0.7 and I am planning to use partial templates. All classes in my application would use same partials so I have to decide where to located all those.

Is it a good idea to put "global" shared partial templates in the lib folder? If no, what is a common practice to choose the folder where to put those? Any advice on how to properly name and load that folder?

Coleman
  • 565
  • 7
  • 15
Backo
  • 18,291
  • 27
  • 103
  • 170

3 Answers3

56

The standard is placing all shared partials in app/views/shared, and referencing them as

render :partial => 'shared/partial_name'

If you have a standard "row in a list" partial (say, for an index page), you could use a shared partial like:

# To render a single object row:
render :partial => 'shared/item', :locals => { :item => @item }
# Or to render them all:
render :partial => 'shared/item', :collection => @items
venables
  • 2,584
  • 2
  • 17
  • 17
  • 4
    Is this still accurate for Rails 4? – James McMahon Nov 05 '13 at 23:24
  • @JamesMcMahon Yes, works with Rails 4. – jlhonora Aug 25 '15 at 14:20
  • what about rails 5??, is there a better way in rails 5 or to use the same – Amrinder Singh Aug 02 '16 at 09:18
  • 1
    No longer true. Rails guide says app/views/application [This makes app/views/application/ a great place for your shared partials, which can then be rendered in your ERB as such](http://guides.rubyonrails.org/layouts_and_rendering.html) – notapatch Nov 10 '16 at 14:59
  • @notapatch Note that all the examples on that page actually use views/shared, e.g. menu, footer, etc. – Barry Kelly Aug 01 '20 at 21:58
  • @BarryKelly - Since I made this comment I found this dhh quote on the subject: It's [app/views/application] a side-effect from this reasonable case: EmployeesController < PeopleController, which will look first for employees/show, then fall back to look for people/show. It was never intended to fall all the way back to look for application/show. That's an unintended side-effect. github.com/rails/rails/pull/9911#issuecomment-15462128 ... end quote. Without better guidance, I'm ok with `shared` or `application`. – notapatch Aug 04 '20 at 19:56
  • This works with Rails 6 – Bcf Ant Aug 21 '20 at 14:49
16

Rails 4:

put the partials you intend to use through out your application in /app/views/application

Then anywhere in your application you can easily:

render partial: 'partial_name', variable_name: variable

The added benefit is that you can always override the partial in a particular view space by redefining what that partial means in /app/views/controller_name/_partial_name.html.erb and the calls to the partial will then reference the more specific context you're in. If that doesn't exist you get the application level partial.

Suggestion taken from Thoughtbot

Charles Smith
  • 301
  • 2
  • 6
  • 1
    This doesn't work when using namespaced controllers. Then it expects the files to be under `app/views/NAMESPACE/application`, without the fallback to non-namespaced – Koen. May 12 '16 at 17:31
  • On second evaluation; this problem only arises when running my specs, but not when just using the application.. – Koen. May 12 '16 at 17:32
4

Conventions is to put them under app/views/shared

If you're going to have many partials, I'd recommend you putting them into subdirectories of that folder, whatever makes sense to your application, since having many partials in one directory is generally not a good practice.

Chubas
  • 17,823
  • 4
  • 48
  • 48