代码之家  ›  专栏  ›  技术社区  ›  sarunw

如何在简单表格gem中使用Bootstrap的输入组

  •  1
  • sarunw  · 技术社区  · 7 年前

    • 红宝石2.5.1
    • 轨道5.2.1
    • 简单表格4.0.1

    当前行为

    http://simple-form-bootstrap.plataformatec.com.br/examples/input_group

    我从 <%= simple_form_for app, wrapper: :input_group do |f| %> 然后得到 Couldn't find wrapper with name input_group ,所以我取消了注释

    config.wrappers :input_group, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b|
        b.use :html5
        b.use :placeholder
        b.optional :maxlength
        b.optional :minlength
        b.optional :pattern
        b.optional :min_max
        b.optional :readonly
        b.use :label, class: 'form-control-label'
        b.wrapper :input_group_tag, tag: 'div', class: 'input-group' do |ba|
          ba.optional :prepend
          ba.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: 'is-valid'
          ba.optional :append
        end
        b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' }
        b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' }
      end
    

    simple_form_bootstrap.rb

    然后得到 undefined method 为类追加' SimpleForm::Inputs::StringInput' 相反。

    有什么问题吗 <%= f.input :subdomain, append: "example.com" %>

    1 回复  |  直到 7 年前
        1
  •  6
  •   PGill    7 年前

    创造 config/initializers/simple_form_component.rb

    module InputGroup
      def prepend(wrapper_options = nil)
        span_tag = content_tag(:span, options[:prepend], class: "input-group-text")
        template.content_tag(:div, span_tag, class: "input-group-prepend")
      end
    
      def append(wrapper_options = nil)
        span_tag = content_tag(:span, options[:append], class: "input-group-text")
        template.content_tag(:div, span_tag, class: "input-group-append")
      end
    end
    
    # Register the component in Simple Form.
    SimpleForm.include_component(InputGroup)
    

    Source