Skip to content

Instantly share code, notes, and snippets.

@mdchaney
Created July 11, 2024 17:26
Show Gist options
  • Save mdchaney/b80ea81b6288b5d281e012dcffdf50fa to your computer and use it in GitHub Desktop.
Save mdchaney/b80ea81b6288b5d281e012dcffdf50fa to your computer and use it in GitHub Desktop.
Bootstrap-styled view generators for Ruby on Rails
<%%= form_for([<%= model_resource_name %>]) do |form| %>
<%%= show_errors_for form.object %>
<% attributes.each do |attribute| -%>
<div class="mb-3">
<% if attribute.password_digest? -%>
<%%= form.label :password, class: "form-label" %>
<%%= form.password_field :password, class: "form-control", required: form.object.new_record? %>
</div>
<div class="mb-3">
<%%= form.label :password_confirmation, class: "form-label" %>
<%%= form.password_field :password_confirmation, class: "form-control", required: form.object.new_record? %>
<% elsif attribute.attachments? -%>
<%%= form.label :<%= attribute.column_name %>, style: "display: block" %>
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true %>
<% elsif attribute.type == :boolean -%>
<div class="form-check">
<%%= form.check_box :<%= attribute.column_name %>, class:"form-check-input" %>
<%%= form.label :<%= attribute.column_name %>, class: "form-check-label" %>
</div>
<% elsif attribute.attachments? -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, multiple: true %>
<% elsif attribute.reference? -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<%%= form.collection_select :<%= attribute.column_name %>, <%= attribute.name.camelize %>.order(:name), :id, :name, { prompt: true }, { class: "form-select" } %>
<% elsif attribute.type == :integer -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<%%= form.number_field :<%= attribute.column_name %>, class: "form-control", required: true, in: 0..999999, step: 1 %>
<% elsif attribute.type == :float -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<%%= form.number_field :<%= attribute.column_name %>, class: "form-control", required: true, in: 0.0..999999.99, step: 0.01 %>
<% elsif attribute.type == :decimal -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<% precision = attribute.attr_options[:precision] || 10 -%>
<% scale = attribute.attr_options[:scale] || 2 -%>
<!-- precision: <%= precision %>, scale: <%= scale %> -->
<%%= form.number_field :<%= attribute.column_name %>, class: "form-control", required: true, in: 0.0..<%= '9' * (precision - scale) %>.<%= '9' * scale %>, step: 0.<%= '0' * (scale - 1) %>1 %>
<% elsif attribute.type == :text -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<%%= form.text_area :<%= attribute.column_name %>, class: "form-control", required: true, rows: 5, maxlength: <%= attribute.attr_options[:limit] || 1000 %> %>
<% elsif attribute.type == :string -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "form-control", required: true, maxlength: <%= attribute.attr_options[:limit] || 100 %> %>
<% else -%>
<%%= form.label :<%= attribute.column_name %>, class: "form-label" %>
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: "form-control", required: true %>
<% end -%>
</div>
<% end -%>
<%%= form.submit class: "btn btn-primary" %>
<%%= button_to "Cancel", <%= index_helper(type: :path) %>, class: "btn btn-secondary" %>
<%% end %>
<h1><%= human_name.pluralize %></h1>
<table id="<%= plural_table_name %>" class="table table-striped">
<thead>
<tr>
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<th><%= attribute.human_name %></th>
<% end -%>
<th>Actions</th>
</tr>
</thead>
<tbody>
<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
<tr id="<%%= dom_id <%= singular_name %> %>">
<% attributes.reject(&:password_digest?).each do |attribute| -%>
<td><%%= <%= singular_name %>.<%= attribute.column_name %> %></td>
<% end -%>
<td class="actions">
<%%= link_to "Show", <%= show_helper(type: :path) %> %> /
<%%= link_to "Edit", <%= edit_helper(type: :path) %> %> /
<%%= link_to "Remove", <%= show_helper(type: :path) %>, data: { turbo_method: :delete, turbo_confirm: "Do you really want to remove this item?" } %>
</td>
</tr>
<%% end %>
</tbody>
</table>
<br>
<%%= link_to "New <%= human_name.downcase %>", <%= new_helper(type: :path) %> %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment