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?
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
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
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.
精彩评论