Browse Source

add simple_form

anxgang 4 years ago
parent
commit
a0ab0d39af
2 changed files with 19 additions and 18 deletions
  1. 10 15
      lib/templates/erb/scaffold/_form.html.erb
  2. 9 3
      template.rb

+ 10 - 15
lib/templates/erb/scaffold/_form.html.erb

@@ -1,4 +1,4 @@
-<%%= form_with(model: <%= model_resource_name %>, local: true) do |form| %>
+<%%= simple_form_for(@<%= singular_table_name %>) do |f| %>
   <%% if <%= singular_table_name %>.errors.any? %>
     <div id="error_explanation">
       <h2><%%= pluralize(<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2>
@@ -11,31 +11,26 @@
     </div>
   <%% end %>
 
-<% attributes.each do |attribute| -%>
-  <div class="form-group">
-<% if attribute.password_digest? -%>
-    <%%= form.label :password %>
-    <%%= form.password_field :password, class: 'form-control' %>
-  </div>
 
-  <div class="form-group">
-    <%%= form.label :password_confirmation %>
-    <%%= form.password_field :password_confirmation, class: 'form-control' %>
+  <div class="form-inputs">
+  <% attributes.each do |attribute| -%>
+<% if attribute.password_digest? -%>
+    <%%= f.input :password %>
+    <%%= f.input :password_confirmation %>
 <% else -%>
-    <%%= form.label :<%= attribute.column_name %> %>
-    <%%= form.<%= attribute.field_type %> :<%= attribute.column_name %>, class: 'form-control' %>
+    <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %>
 <% end -%>
+  <% end -%>
   </div>
 
-<% end -%>
-  <div class="form-group">
+  <div class="form-actions">
     <%% if <%= model_resource_name %>.persisted? %>
       <div class="float-right">
         <%%= link_to 'Destroy', <%= model_resource_name %>, method: :delete, class: "text-danger", data: { confirm: 'Are you sure?' } %>
       </div>
     <%% end %>
 
-    <%%= form.submit class: 'btn btn-primary' %>
+    <%%= f.submit class: 'btn btn-primary' %>
 
     <%% if <%= model_resource_name %>.persisted? %>
       <%%= link_to "Cancel", <%= model_resource_name %>, class: "btn btn-link" %>

+ 9 - 3
template.rb

@@ -52,10 +52,11 @@ def add_gems
   gem 'sidekiq', '~> 5.2', '>= 5.2.5'
   gem 'sitemap_generator', '~> 6.0', '>= 6.0.1'
   gem 'whenever', require: false
+  gem 'simple_form', '~> 4.1.0'
 
   gem_group :development do
-    gem 'pry'
-    gem 'bullet'
+    gem 'pry', '~> 0.12.2'
+    gem 'bullet', '~> 6.0.0'
   end
 
   if rails_5?
@@ -241,6 +242,10 @@ def add_friendly_id
   )
 end
 
+def add_simple_form
+  generate "simple_form:install --bootstrap"
+end
+
 def stop_spring
   run "spring stop"
 end
@@ -293,13 +298,14 @@ after_bundle do
   add_multiple_authentication
   add_sidekiq
   add_friendly_id
+  add_simple_form
 
   copy_templates
   add_whenever
   add_sitemap
 
   add_bullet
-
+  
   # Migrate
   rails_command "db:create"
   rails_command "db:migrate"