My app has objects Foo and Bar, which each have corresponding models, views and controllers.
A form partial comes standard after generating scaffolding for each object.
Foo form partial looks something 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 something 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 and I want it to be easy to maintain, but the objects have different attributes and different numbers of attributes. Is there a way for me to DRY out my formatting?
Since you have a repeating structure for rendering you could extract that into a partial and then pass the variables for rendering. So your partial looks like
<div class="form-inputs row">
<div class="col-md-6">
<% vals.each do |val| %>
<%= val %>
<% end %>
</div>
</div>
And in your main view you pass the variables for rendering.
<%= render 'home', vals: [Foo.biscuit, Foo.bagel] %>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.