Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which folder should I put "global" shared partial templates? [duplicate]

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?

like image 890
Backo Avatar asked Jul 01 '11 15:07

Backo


2 Answers

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 
like image 97
venables Avatar answered Oct 10 '22 01:10

venables


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

like image 40
Charles Smith Avatar answered Oct 10 '22 01:10

Charles Smith