ruby-on-rails - 如何在 simple_form gem 中使用 Bootstrap 的输入组

标签 ruby-on-rails simple-form simple-form-for

环境

  • ruby 2.5.1
  • rails 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" %>
    我该如何使用它,它甚至支持 simple_form 吗?不知道他们为什么注释掉那块代码。

    最佳答案

    创建 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

    关于ruby-on-rails - 如何在 simple_form gem 中使用 Bootstrap 的输入组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52342650/

    相关文章:

    ruby-on-rails - RSpec 访问 application_controller 方法如 'current_user'

    ruby-on-rails - 使用特定日期格式的键检索哈希值

    ruby-on-rails - 如何在 Ruby on rails 中压缩 css 文件和 javascript 文件

    ruby-on-rails - 有没有一种方法可以使用simple_form for Rails提交ajax/json请求

    ruby-on-rails - 使用简单形式的 gem f.association 为新操作提供 NoMethodError

    mysql - Rails 表索引不是预期的数据库中的外键 - 为什么?

    mysql - 如何在 SQL/Rails 应用程序中删除 TIME 数据类型中的分钟

    ruby-on-rails-3 - 有没有办法在 show.html.erb 中使用 simple_form

    html - 样式的简单形式