html - 自定义 simple_form 包装器以匹配 Bootstrap 4 复选框

标签 html ruby-on-rails twitter-bootstrap forms simple-form

我正在尝试创建一个 simple_form 包装器来匹配 Bootstrap 4 的堆叠复选框/单选按钮。

这是我想要复制的 HTML 结构,由 Boostrap's docs 提供:

<div class="form-check">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox" value="">
    Option one is this and that&mdash;be sure to include why it's great
  </label>
</div>
<div class="form-check disabled">
  <label class="form-check-label">
    <input class="form-check-input" type="checkbox" value="" disabled>
    Option two is disabled
  </label>
</div>

这是我的 simple_form 包装器当前所在的位置:

config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
  b.use :html5
  b.optional :readonly
  b.use :label
  b.use :input, class: "form-check-input"
  b.use :error, wrap_with: { tag: 'div', class: 'invalid-feedback' }
  b.use :hint,  wrap_with: { tag: 'small', class: 'form-text text-muted' }
end

这是当前输出的 HTML 结构:

<span class="checkbox">
  <label for="">
    <input class="form-check-input check_boxes optional" type="checkbox" value="" name="" id="">
    Text for checkbox goes here
  </label>
</span>

最佳答案

更新 05/2018

SimpleForm 发布了新版本 ships a Bootstrap 4.1 generator 。确保安装了 4.0.0+ 版本:

gem 'simple_form', '~> 4.0.0'

然后您可以使用以下命令生成新的初始化程序文件:

rails g simple_form:install --bootstrap

请确保使用 Bootstrap 4.1,但存在一些细微的差异。

上一个答案

深入挖掘 source code 后,我找到了需要传递给包装器配置的相关选项 item_wrapper_classitem_wrapper_label:

 config.wrappers(:vertical_radio_and_checkboxes, tag: 'div', 
    class: 'form-group', 
    error_class: 'has-error',
    item_wrapper_class: 'form-check', 
    item_label_class: 'form-check-label') do |b|
   b.use :html5
   b.optional :readonly
   b.use :label, class: 'form-control-label'
   b.use :input, class: 'form-check-input'
   b.use :error, wrap_with: { tag: 'div', class: 'invalid-feedback' }
   b.use :hint,  wrap_with: { tag: 'small', class: 'form-text text-muted' }
end

关于html - 自定义 simple_form 包装器以匹配 Bootstrap 4 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46063574/

相关文章:

javascript - JQuery UI 菜单防止子菜单项崩溃?

html - Bootstrap 中的类别内联 block "buttons"

javascript - 选择座位时未定义为元素 ID

html - 有没有办法允许用户从网站复制文本,并且文本是未格式化的?

java XMLWorkerHelper - NoSuchMethodError : com. itextpdf.text.pdf.PdfPTable.addCell

ruby-on-rails - Ruby on Rails Web 应用程序的最佳 CI 服务器是什么?

html - Bootstrap 3 : Emails defaulting to print media type even on screen

html - 位置固定抛出背景图像

ruby-on-rails - 如何使用 Devise 锁定用户?

ruby-on-rails - 使用 RSpec 测试 AJAX 密集型 Rails 应用程序