Bootstrap Scaffold Templates
Overrides the default scaffold templates with Bootstrap.
Used 124 times
R
Ricardo Villagrana
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/VqqsG8"
Template Source
Review the code before running this template on your machine.
file 'lib/templates/erb/scaffold/index.html.erb.tt', <<-CODE
<%% if notice.present? %>
<p id="notice" class="alert alert-primary"><%%= notice %></p>
<%% end %>
<div class="d-flex justify-content-between align-items-baseline">
<h1><%= plural_table_name.capitalize %></h1>
<%%= link_to "New <%= singular_table_name %>", new_<%= singular_table_name %>_path, class: 'btn btn-primary' %>
</div>
<table class="table table-hover">
<thead>
<tr>
<% attributes.each do |attribute| %>
<th><%= attribute.human_name %></th>
<% end %>
<th></th>
</tr>
</thead>
<tbody>
<%% @<%= plural_table_name%>.each do |<%= singular_table_name %>| %>
<%%= content_tag :tr, id: dom_id(<%= singular_table_name %>), class: dom_class(<%= singular_table_name %>) do %>
<% attributes.each do |attribute| %>
<td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td>
<% end %>
<td>
<div class="btn-group" role="group">
<%%= link_to 'Show', <%= singular_table_name %>, class: 'btn btn-outline-primary' %>
<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>), class: 'btn btn-outline-dark' %>
<%%= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-outline-danger' %>
</div>
</td>
<%% end %>
<%% end %>
</tbody>
</table>
CODE
file 'lib/templates/erb/scaffold/show.html.erb.tt', <<-CODE
<h1>Show <%= singular_table_name.capitalize %></h1>
<%% if notice.present? %>
<p id="notice" class="alert alert-primary"><%%= notice %></p>
<%% end %>
<div class="card p-4">
<ul class="list-group mb-2">
<% attributes.each do |attribute| %>
<li class="list-group-item">
<strong><%= attribute.human_name %>:</strong>
<%%= @<%= singular_table_name %>.<%= attribute.name %> %>
</li>
<% end %>
</ul>
<div>
<%%= link_to 'Back', <%= index_helper %>_path, class: 'btn btn-dark mr-2' %>
<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>), class: 'btn btn-outline-dark' %>
</div>
</div>
CODE
file 'lib/templates/erb/scaffold/new.html.erb.tt', <<-CODE
<h1>New <%= singular_table_name.capitalize %></h1>
<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
CODE
file 'lib/templates/erb/scaffold/edit.html.erb.tt', <<-CODE
<h1>Edit <%= singular_table_name.capitalize %></h1>
<%%= render 'form', <%= singular_table_name %>: @<%= singular_table_name %> %>
CODE
file 'lib/templates/erb/scaffold/_form.html.erb.tt', <<-CODE
<%%= form_with(model: <%= singular_table_name %>, local: true) do |form| %>
<%% if <%= singular_table_name %>.errors.any? %>
<div id="error_explanation" class="alert alert-danger" role="alert">
<p class="font-weight-bold">
<%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:
</p>
<ul>
<%% <%= singular_table_name %>.errors.full_messages.each do |message| %>
<li><%%= message %></li>
<%% end %>
</ul>
</div>
<%% end %>
<div class="card mx-auto p-4">
<% attributes.each do |attribute| %>
<div class="mb-3">
<%%= form.label :<%= attribute.name %>, class: 'form-label' %>
<%%= form.text_field :<%= attribute.name %>, class: 'form-control' %>
</div>
<% end %>
<div class="d-flex justify-content-between">
<div>
<%%= link_to 'Back', <%= index_helper %>_path, class: 'btn btn-dark mr-2' %>
<%%= link_to 'Show', <%= singular_table_name %>_path, class: 'btn btn-outline-primary mr-2' if <%= singular_table_name %>.persisted? %>
</div>
<%%= form.submit class: 'btn btn-primary' %>
</div>
</div>
<%% end %>
CODE
Comments
yashm
Does not currently work on Rails 6.1 https://github.com/rails/rails/issues/41235