程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了是否可以覆盖 SimpleForm 生成的表单字段的基“类”?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到是否可以覆盖 SimpleForm 生成的表单字段的基“类”?的问题如何解决?下面主要结合日常开发的经验,给出你关于是否可以覆盖 SimpleForm 生成的表单字段的基“类”?的解决方法建议,希望对你解决是否可以覆盖 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

大佬总结

以上是大佬教程为你收集整理的是否可以覆盖 SimpleForm 生成的表单字段的基“类”?全部内容,希望文章能够帮你解决是否可以覆盖 SimpleForm 生成的表单字段的基“类”?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: