Bootstrap 3 + simple_forms复选框

caa*_*os0 13 simple-form twitter-bootstrap-3

我正在尝试将bootstrap 3与simple_forms(来自master)集成.

现在,我有以下内容:

配置/初始化/ simple_form.rb:

SimpleForm.setup do |config|
  config.wrappers :default, class: :input,
    hint_class: :field_with_hint, error_class: :field_with_errors do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label_input
    b.use :hint,  wrap_with: { tag: :span, class: :hint }
    b.use :error, wrap_with: { tag: :span, class: :error }
  end

  config.default_wrapper = :default
  config.boolean_style = :nested
  config.button_class = 'btn'
end
Run Code Online (Sandbox Code Playgroud)

配置/初始化/ simple_form_bootstrap.rb:

SimpleForm.setup do |config|
  config.input_class = 'form-control'

  config.wrappers :bootstrap, tag: 'div', class: 'form-group', error_class: 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper tag: 'div', class: 'controls' do |ba|
      ba.use :input
      ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
      ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end

  config.wrappers :prepend, tag: 'div', class: "form-group", error_class: 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper tag: 'div', class: 'controls' do |input|
      input.wrapper tag: 'div', class: 'input-prepend' do |prepend|
        prepend.use :input
      end
      input.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
      input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
    end
  end

  config.wrappers :append, tag: 'div', class: "form-group", error_class: 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper tag: 'div', class: 'controls' do |input|
      input.wrapper tag: 'div', class: 'input-append' do |append|
        append.use :input
      end
      input.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
      input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
    end
  end

  config.default_wrapper = :bootstrap
end
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/设计/会话/ new.html.haml

%div.panel.panel-auth
  %div.panel-heading
    %h3.panel-title Sign in
  %div.panel-body
    = simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
      .form-inputs
        = f.input :email, :required => false, :autofocus => true
        = f.input :password, :required => false
        = f.input :remember_me, :as => :boolean if devise_mapping.rememberable?
      .form-actions
        = f.button :submit, "Sign in"
      %hr
    = render "devise/shared/links"
Run Code Online (Sandbox Code Playgroud)

但生成的HTML是错误的.嗯,根据BS2,它是正确的,但BS3是错误的.这里是:

<div class="form-group boolean optional user_remember_me">
  <label class="boolean optional control-label" for="user_remember_me">
    Remember me
  </label>
  <div class="controls">
    <input name="user[remember_me]" type="hidden" value="0">
    <label class="checkbox">
      <input class="boolean optional form-control" id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
    </label>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

但它实际应该是这样的:

  <div class="checkbox">
    <label>
      <input type="checkbox"> Check me out
    </label>
  </div>
Run Code Online (Sandbox Code Playgroud)

可能有可能解决这个黑客周围的包装,但我无法让它工作.有人可以给我一些建议吗?

干杯

mwa*_*her 12

就像你说的,你可以使用自定义包装器:

 config.wrappers :checkbox, tag: :div, class: "checkbox", error_class: "has-error" do |b|

    # Form extensions
    b.use :html5

    # Form components
    b.wrapper tag: :label do |ba|
      ba.use :input
      ba.use :label_text
    end

    b.use :hint,  wrap_with: { tag: :p, class: "help-block" }
    b.use :error, wrap_with: { tag: :span, class: "help-block text-danger" }
  end
Run Code Online (Sandbox Code Playgroud)

然后您在输入中引用:

 = f.input :remember_me, :as => :boolean, :wrapper => :checkbox if devise_mapping.rememberable?
Run Code Online (Sandbox Code Playgroud)

但请注意,这不适用于collection_check_boxes:

= f.input :roles, as: :check_boxes, wrapper: :checkbox, collection: @employee_roles, label: false
Run Code Online (Sandbox Code Playgroud)

您可以尝试将后一种情况的自定义输入混合在一起,但它有点混乱.也许其他人知道更好的方法......也许simple_form很快就能赶上bootstrap 3.

  • 没用.它仍然添加了"表单控制"类,这会搞砸一切.:/ (4认同)
  • 这对我有用,谢谢!您不必为每个输入字段指定:wrapper =>:checkbox,只需将其添加到simple_form初始化器的顶部:config.wrapper_mappings = {:boolean =>:checkbox} (4认同)