使用SimpleForm和Bootstrap 3?

Dan*_*hoe 6 ruby-on-rails simple-form twitter-bootstrap twitter-bootstrap-3

我有一个使用SimpleForm gem的Rails应用程序.当前的SimpleForm 3.0.0.rc版本使用Bootstrap 2.3提供了引人注目的表单样式.但是当我使用Bootstrap 3.0时,我失去了漂亮的表单样式.如何在Bootstrap 3中使用SimpleForm?

tok*_*olt 8

https://github.com/plataformatec/simple_form/issues/857 - 这可能会有所帮助.

您需要更改简单的表单初始化程序.是我在我的一个项目中使用的内容

UPD 2014年4月22日

简单形式3.1.0.rc1,支持Bootstrap 3.


cod*_*oob 8

这个要点:

https://gist.github.com/tokenvolt/6599141

对我很有帮助!

编辑 - 由于主持人评论不喜欢仅链接响应,因此将要点的当前内容粘贴到SO中.但是,最好从要点中提取它.

非常感谢@Tokenvolt

inputs = %w[
  CollectionSelectInput
  DateTimeInput
  FileInput
  GroupedCollectionSelectInput
  NumericInput
  PasswordInput
  RangeInput
  StringInput
  TextInput
]

inputs.each do |input_type|
  superclass = "SimpleForm::Inputs::#{input_type}".constantize

  new_class = Class.new(superclass) do
    def input_html_classes
      super.push('form-control')
    end
  end

  Object.const_set(input_type, new_class)
end

# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
  config.boolean_style = :nested

  config.wrappers :bootstrap3, tag: 'div', class: 'form-group', error_class: 'has-error',
      defaults: { input_html: { class: 'default_class' } } do |b|

    b.use :html5
    b.use :min_max
    b.use :maxlength
    b.use :placeholder

    b.optional :pattern
    b.optional :readonly

    b.use :label_input
    b.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
    b.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' }
  end

  config.wrappers :prepend, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.wrapper tag: 'div', class: 'controls' do |input|
      input.wrapper tag: 'div', class: 'input-group' do |prepend|
    prepend.use :label , class: 'input-group-addon' ###Please note setting class here fro the label does not currently work (let me know if you know a workaround as this is the final hurdle)
        prepend.use :input
      end
      input.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
      input.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' }
    end
  end

  config.wrappers :append, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.wrapper tag: 'div', class: 'controls' do |input|
      input.wrapper tag: 'div', class: 'input-group' do |prepend|
        prepend.use :input
    prepend.use :label , class: 'input-group-addon' ###Please note setting class here fro the label does not currently work (let me know if you know a workaround as this is the final hurdle)
      end
      input.use :hint,  wrap_with: { tag: 'span', class: 'help-block' }
      input.use :error, wrap_with: { tag: 'span', class: 'help-block has-error' }
    end
  end

  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

  # Wrappers for forms and inputs using the Twitter Bootstrap toolkit.
  # Check the Bootstrap docs (http://getbootstrap.com/)
  # to learn about the different styles for forms and inputs,
  # buttons and other elements.
  config.default_wrapper = :bootstrap3
end
Run Code Online (Sandbox Code Playgroud)

  • 虽然此链接可能会回答这个问题,但最好在此处包含答案的基本部分并提供参考链接.如果链接的页面发生更改,则仅链接的答案可能会无效. (2认同)

trh*_*trh 1

为您的项目添加一个简单形式的初始值设定项 ( config/initializers/simple_form.rb) 并放入以下要点的内容:

https://gist.github.com/tommarshall/6308327/0141a600a93a1711d4762a04dd0d85a3ee14041e

我刚刚经历过这个,这个要点围绕引导新的布局/类名工作。