Bootstrap 5 Scaffold Templates
Overrides the default ERB templates with Bootstrap 5 friendly versions.
Used 374 times
D
Dale Zak
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/XbBs3y"
Template Source
Review the code before running this template on your machine.
file 'lib/templates/erb/scaffold/index.html.erb.tt', <<-CODE
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active" aria-current="page"><%= plural_table_name.capitalize %></li>
</ol>
</nav>
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 row-cols-xl-4 g-4">
<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %>
<div class="col">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title"><%%= link_to <%= singular_table_name %>.id, <%= model_resource_name %> %></h5>
<p class="card-text"><%%= <%= singular_table_name %>.inspect %></p>
</div>
<div class="card-footer">
<small class="text-muted"><%%= <%= singular_table_name %>.created_at %></small>
</div>
</div>
</div>
<%% end %>
</div>
CODE
file 'lib/templates/erb/scaffold/show.html.erb.tt', <<-CODE
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><%%= link_to "<%= plural_table_name.capitalize %>", <%= index_helper %>_path %></li>
<li class="breadcrumb-item active mr-auto" aria-current="page"><%%= @<%= singular_table_name %>.id %></li>
</ol>
</nav>
<div class="card">
<h5 class="card-header"><%= singular_table_name.capitalize %></h5>
<ul class="list-group list-group-flush">
<% attributes.reject{|a| a.name == 'type' || a.name == 'deleted_at' || a.name == 'password_digest'}.each do |attribute| -%>
<li class="list-group-item">
<p class="fw-bold"><%= attribute.human_name %></p>
<p class="fw-normal"><%%= @<%= singular_table_name %>.<%= attribute.name %> %></p>
</li>
<% end -%>
</ul>
</div>
CODE
file 'lib/templates/erb/scaffold/new.html.erb.tt', <<-CODE
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><%%= link_to "<%= plural_table_name.capitalize %>", <%= index_helper %>_path %></li>
<li class="breadcrumb-item active" aria-current="page">Add</li>
</ol>
</nav>
<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
CODE
file 'lib/templates/erb/scaffold/edit.html.erb.tt', <<-CODE
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><%%= link_to "<%= plural_table_name.capitalize %>", <%= index_helper %>_path %></li>
<li class="breadcrumb-item"><%%= link_to @<%= singular_table_name %>.id, @<%= singular_table_name %> %></li>
<li class="breadcrumb-item active" aria-current="page">Edit</li>
</ol>
</nav>
<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
CODE
file 'lib/templates/erb/scaffold/_form.html.erb.tt', <<-CODE
<%% if <%= singular_table_name %>.errors.any? %>
<%% <%= singular_table_name %>.errors.full_messages.each do |message| %>
<div class="alert alert-danger" role="alert">
<%%= message %>
</div>
<%% end %>
<%% end %>
<%%= form_with(model: <%= model_resource_name %>, local: true) do |form| -%>
<div class="card">
<h5 class="card-header"><%= singular_table_name.capitalize %></h5>
<ul class="list-group list-group-flush">
<% attributes.reject{|a| a.name == 'type' || a.name == 'deleted_at' || a.name.end_with?("_count")}.each do |attribute| -%>
<li class="list-group-item">
<div class="form-group">
<% if attribute.password_digest? -%>
<%%= form.label :password, class: "font-weight-bold" %>
<%%= form.password_field :password, placeholder: "Enter password", class: "form-control" %>
<%%= form.label :password_confirmation, class: "fw-bold" %>
<%%= form.password_field :password_confirmation, placeholder: "Enter password confirmation", class: "form-control" %>
<% else -%>
<%%= form.label :<%= attribute.column_name %>, class: "fw-bold" %>
<%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, placeholder: "Enter <%= attribute.column_name %>", class: "form-control" %>
<% end -%>
</div>
</li>
<% end -%>
</ul>
<div class="card-footer">
<%%= link_to "Cancel", <%= model_resource_name %>, class: 'btn btn-outline-secondary float-start' if <%= singular_table_name %>.persisted? %>
<%%= link_to "Cancel", <%= index_helper %>_path, class: 'btn btn-outline-secondary float-start' if !<%= singular_table_name %>.persisted? %>
<%%= form.submit "Save", class: "btn btn-primary float-end" if <%= singular_table_name %>.persisted? %>
<%%= form.submit "Create", class: "btn btn-primary float-end" if !<%= singular_table_name %>.persisted? %>
</div>
</div>
<%% end %>
CODE