【问题标题】:SimpleForm default input classSimpleForm 默认输入类
【发布时间】:2013-04-24 04:44:29
【问题描述】:

我正在使用 SimpleForm + Bootstrap。如何使用 class= span12 向所有 type="text" 输入添加属性?

输出类似这样的东西:

<div class="controls"><input autofocus="autofocus" class="string required span12" id="user_first_name" name="user[first_name]" required="required" size="50" type="text" value=""></div>

我试过玩 config.wrappers 但是这个

ba.use :input,  :wrap_with => { :class => 'span12' }

不起作用。它添加到包装器而不是修改输入标签。有什么想法吗?

SimpleForm.setup do |config|
  config.wrappers :bootstrap, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper :tag => 'div', :class => 'controls' do |ba|
      ba.use :input
      ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
      ba.use :hint,  :wrap_with => { :tag => 'p', :class => 'help-block' }
    end
  end
  config.default_wrapper = :bootstrap
end

【问题讨论】:

    标签: ruby-on-rails-3 simple-form


    【解决方案1】:

    您可以通过将 string_input.rb 类文件添加到您的 Rails 加载路径(即 app/inputs/string_input.rb)来简单地覆盖字符串输入的 simple_form 默认值,并包括以下内容:

    class StringInput < SimpleForm::Inputs::StringInput
      def input_html_classes
        super.push('span12')
      end
    end
    

    如果您需要为其他类型的输入添加更多默认类,您可以查看提供的各种输入类型:

    https://github.com/plataformatec/simple_form/tree/master/lib/simple_form/inputs

    【讨论】:

    • 我想这可以作为临时解决方案,直到配置允许为止。
    • 对于那些通过谷歌到达这里的人,现在支持从 v2.2 开始(参见 changelog),带有 config.input_class 调用,但这也将容器 div 包装在同一个类中,这使得引导样式变得不稳定。因此,即使配置允许在所有输入上设置类,这个答案仍然可能是最好的解决方案。
    【解决方案2】:

    我遇到了类似的问题,经过一些研究后我发现使用 wrapper api 不支持此问题。

    您可以做的最好的事情是在每个表单中使用 :defaults 选项,而不是为每个输入添加。

    https://github.com/plataformatec/simple_form/issues/754

    您可以这样实现:simple_form_for(@my_instance, defaults: { input_html: { class: 'span12' }})

    您还可以选择使用自定义表单生成器。 https://github.com/plataformatec/simple_form#custom-form-builder

    【讨论】:

      猜你喜欢
      • 2019-01-25
      • 2020-11-23
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多