通过新建lib/templates/erb/scaffold/_form.html.erb,来重写scaffold生成view下_form.html.erb的默认模板
Bootstrap: Basic Form:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%%= simple_form_for(@<%= singular_table_name %>) do |f|%> <%%= f.error_notification%> <div class="col-md-6"> <div class="form-inputs"> <%- attributes.each do |attribute| -%> <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>%> <%- end -%> </div> </div> <div class="form-actions"> <%%= f.button :submit %> </div> <%% end %>
Simple Form: Inline Form
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%%= simple_form_for @<%= singular_table_name%>, wrapper: :inline_form, html: { class: 'form-inline' } do |f| %> <%%= f.error_notification %> <div class="col-md-6"> <div class="form-inputs"> <%- attributes.each do |attribute| -%> <%%= f.<%= attribute.reference? ? :association : :input%>:<%= attribute.name%>, placeholder: 'Enter <%= attribute.name%>'%> <%- end -%> <%%=f.button :submit %> </div> </div> <%% end%>
Simple Form: Horizontal Form
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%%= simple_form_for @<%= singular_table_name%>, as: 'user_horizontal', html: { class: 'form-horizontal' }, wrapper: :horizontal_form, wrapper_mappings: { check_boxes: :horizontal_radio_and_checkboxes, radio_buttons: :horizontal_radio_and_checkboxes, file: :horizontal_file_input, boolean: :horizontal_boolean } do |f| %> <%%= f.error_notification %> <div class="col-md-6"> <div class="form-inputs"> <%- attributes.each do |attribute| -%> <%%= f.<%= attribute.reference? ? :association : :input%>:<%= attribute.name%>, placeholder: '<%= attribute.name%>'%> <%- end -%> <%%= f.input :file, as: :file %> <%%= f.input :sex, as: :radio_buttons, collection: ["Male", "Female"] %> </div> </div> <%%= f.button :submit %> <%% end %>
简略版本
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<%%= simple_form_for @<%= singular_table_name%>, as: 'user_horizontal', html: { class: 'form-horizontal' }, wrapper: :horizontal_form do |f| %> <%%= f.error_notification %> <div class="col-md-6"> <div class="form-inputs"> <%- attributes.each do |attribute| -%> <%%= f.<%= attribute.reference? ? :association : :input%>:<%= attribute.name%>, placeholder: '<%= attribute.name%>'%> <%- end -%> </div> </div> <%%= f.button :submit %> <%% end %>
asfd
asdf
show.html.haml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
.wrapper.wrapper-content.animated.fadeInRight.ecommerce .ibox .ibox-title %h5 配送平台详情 .ibox-content.header .row <%- attributes.each do |attribute| -%> .col-md-6.form-group %b <%= attribute.name %> \: = @<%= singular_table_name %>.<%= attribute.name %> <%- end -%> .clearfix = link_to '<%= singular_table_name %>设置', edit_<%= singular_table_name %>_path, class: 'btn btn-primary'
edit.html.haml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
.ibox .ibox-title %h5 <%= singular_table_name %>设置 .ibox-content = render 'form' .row = link_to t('Show'), <%= singular_table_name %>_path, :class=>"btn btn-default"
_form.html.haml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
= simple_form_for(@<%= singular_table_name %>, url: backend_<%= table_name %>_path) do |f| - if @<%= singular_table_name %>.errors.any? #error_explanation %h2= "#{pluralize(@<%= singular_table_name %>.errors.count, "error")} prohibited this bank account from being saved:" %ul - @<%= singular_table_name %>.errors.full_messages.each do |msg| %li= msg .form-inputs <%- attributes.each do |attribute| -%> .col-sm-6 = f.input :<%= attribute.name %> <%- end -%> .clearfix .form-actions .col-sm-6 .col-xs-offset-2 = f.button :submit, :id => 'submit-button'
index.html.haml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
.row.wrapper.border-bottom.white-bg.page-heading .col-lg-10 %h2= "<%= singular_table_name %>" .wrapper.wrapper-content.animated.fadeInRight.ecommerce .row .col-lg-12 .ibox .ibox-content %table.table.table-striped %thead %tr <%- attributes.each do |attribute| -%> %th.col-sm-2 <%= attribute.name %> <%- end -%> %tbody - @<%= table_name %>.each do |<%= singular_table_name %>| %tr <%- attributes.each do |attribute| -%> %td= <%= singular_table_name %>.<%= attribute.name %> <%- end -%> %td = link_to t('配置'), url_for(:controller => "<%= table_name %>", :action => "edit", :id => <%= singular_table_name %>.id)
new.html.haml
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
.wrapper.wrapper-content.animated.fadeInRight.ecommerce .row .col-lg-12 .ibox .ibox-title %h2= "新建<%= singular_table_name %>" .ibox-content = render 'form' = link_to t('Back'), <%= table_name %>_path, :class => "btn btn-default btn-sm"