是否可以覆盖 SimpleForm 生成的表单字段的基“类”?

问题描述

这是我的simple_form.rb

  config.wrappers :select_form,tag: "div",class: "control" do |b|
    b.use :html5
    b.use :placeholder
    b.optional :pattern
    b.optional :readonly
    b.use :label,class: "label"
    b.use :input,wrap_with: { tag: "div",class: "select" }
    b.use :full_error,class: "help is-danger" }
    b.use :hint,wrap_with: { tag: "small",class: "form-text text-muted" }
  end

产生这个:

<div class="control select required user_role"><label class="label select required" for="user_role">User Role</label>
    <div class="select"><select class="select required" required="required" aria-required="true" name="user[role]" id="user_role">

问题是 SimpleForm 和我的 CSS 类 Bulma 都使用 .select 类来确定元素的类型,从而导致图形冲突。

是否有必要从父类 select删除<div>,因为它没有必要?

解决方法

将此添加到我的 initializers/simple_form.rb 中:

class SimpleForm::Wrappers::Many
  def wrap(input,options,content)
    return content if options[namespace] == false
    return if defaults[:unless_blank] && content.empty?

    tag = (namespace && options[:"#{namespace}_tag"]) || @defaults[:tag]
    return content unless tag

    klass = html_classes(input,options)

    opts  = html_options(options)

    # Removes the conflict class ".select" from the wrapper
    klass.delete(:select) if options[:wrapper] == :select_form && klass.length > 1
    opts[:class] = (klass << opts[:class]).join(' ').strip unless klass.empty?
    input.template.content_tag(tag,content,opts)
  end
end