Rails 4: how can I DRY out my multiple form partials? -
my app has objects foo , bar, each have corresponding models, views , controllers.
a form partial comes standard after generating scaffolding each object.
foo form partial looks like...
<div class="form-inputs row"> <div class="col-md-6"> <%= foo.donut %> <%= foo.bagel %> <%= foo.cookie %> </div> </div> <div class="form-inputs row"> <div class="col-md-6"> <%= foo.biscuit %> <%= foo.toast %> <%= foo.muffin %> </div> </div>
bar form partial looks like...
<div class="form-inputs row"> <div class="col-md-6"> <%= bar.cat %> </div> </div> <div class="form-inputs row"> <div class="col-md-6"> <%= bar.dog %> </div> </div>
i want uniform formatting , want easy maintain, objects have different attributes , different numbers of attributes. there way me dry out formatting?
since have repeating structure rendering extract partial , pass variables rendering. partial looks like
<div class="form-inputs row"> <div class="col-md-6"> <% vals.each |val| %> <%= val %> <% end %> </div> </div>
and in main view pass variables rendering.
<%= render 'home', vals: [foo.biscuit, foo.bagel] %>
Comments
Post a Comment