Ruby-on-rails 如何在simple_form 2中的包装器中向输入组件添加类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9371490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:52:04  来源:igfitidea点击:

how to add a class to the input component in a wrapper in simple_form 2

ruby-on-railssimple-form

提问by Nik So

I am trying to have class="text"in my input fields when using a custom wrapper called :hinted in simple_form 2.0.0.rc

我试图class="text"在我的输入字段中使用名为 :hinted in simple_form 2.0.0.rc 的自定义包装器

config.wrappers :hinted do |b|
  b.use :input, :class => "text"
end

but the output doesn't have that class, I tried

但输出没有那个类,我试过了

:wrap_with => {:class => 'text'} 

to no avail

无济于事

Does anyone know how this is done?

有谁知道这是怎么做的?

Thank You!

谢谢你!

回答by Rimian

With :input_html works. It is a bit clunky.

随着 :input_html 作品。它有点笨重。

= f.input :email, :input_html => { :class => 'foo' }

You can also set all the inputs on all the form elements:

您还可以在所有表​​单元素上设置所有输入:

simple_form_for(@user, :defaults => { :input_html => { :class => "foo" } })

But as you'd expect, this applies to everything.

但正如您所料,这适用于一切。

You can create custom form elements:

您可以创建自定义表单元素:

# app/inputs/foo_input.rb
class FooInput < SimpleForm::Inputs::StringInput
  def input_html_classes
    super.push('foo')
  end
end

// in your view:
= f.input :email, :as => :foo

See: https://github.com/plataformatec/simple_form/wiki/Adding-custom-input-components

请参阅:https: //github.com/plataformatec/simple_form/wiki/Adding-custom-input-components

You can also create a custom form builder:

您还可以创建自定义表单构建器:

def custom_form_for(object, *args, &block)
  options = args.extract_options!
  simple_form_for(object, *(args << options.merge(builder: CustomFormBuilder)), &block)
end

class CustomFormBuilder < SimpleForm::FormBuilder
  def input(attribute_name, options = {}, &block)
    options[:input_html].merge! class: 'foo'
    super
  end
end

回答by rafaelfranca

Currently there no way to do this. You can use the defaultsoptions like this if you want.

目前没有办法做到这一点。defaults如果需要,您可以使用这样的选项。

<%= simple_form_for(@user, :defaults => { :input_html => { :class => "text" } }) do %>
  <%= f.input :name %>
<% end %>

回答by crispy

This feature is about to be merged to master right now (Oct. 2012):

此功能现在即将合并到 master(2012 年 10 月):

https://github.com/plataformatec/simple_form/pull/622

https://github.com/plataformatec/simple_form/pull/622

Then you can do something like this to add HTML attributes directly on the input field:

然后你可以做这样的事情来直接在输入字段上添加 HTML 属性:

SimpleForm.build :tag => :div, :class => "custom_wrapper" do |b|
  b.wrapper :tag => :div, :class => 'elem' do |component|
    component.use :input, :class => ['input_class_yo', 'other_class_yo']
    component.use :label, :"data-yo" => 'yo'
    component.use :label_input, :class => 'both_yo'
    component.use :custom_component, :class => 'custom_yo'
  end
end

回答by Johan Tique

I had a similar problem, however it seems that this feature (the input_classone) was merged after the 3.0.0 version.

我有一个类似的问题,但是似乎这个功能(input_class一个)是在 3.0.0 版本之后合并的。

So I tried to make a monkey patch for supporting at least the config.input_class = 'foo'code

所以我尝试制作一个猴子补丁来至少支持config.input_class = 'foo'代码

My intention is not to do a great monkey patch (in fact I like this article herefor doing that - the monkey patch), well it is only an idea but it works, now I'm working with the SimpleForm v2.1.3 and Bootstrap 4 - alpha version (the last one is not important here but it is just for an information purpose)

我的意图不是做一个伟大的猴子补丁(事实上我喜欢这篇文章在这里做 - 猴子补丁),好吧,这只是一个想法,但它有效,现在我正在使用 SimpleForm v2.1.3 和 Bootstrap 4 - alpha 版本(最后一个在这里并不重要,但它只是为了提供信息)

here is the code for the monkey patch:

这是猴子补丁的代码:

module SimpleForm
  mattr_accessor :input_class
  @@input_class = nil
end
module SimpleForm
  module Inputs
    class Base
      def html_options_for(namespace, css_classes)
        html_options = options[:"#{namespace}_html"]
        html_options = html_options ? html_options.dup : {}
        css_classes << html_options[:class] if html_options.key?(:class)
        css_classes << SimpleForm.input_class if namespace == :input && SimpleForm.input_class.present?
        html_options[:class] = css_classes unless css_classes.empty?
        html_options
      end
    end
  end
end

now you can do something like this:

现在你可以做这样的事情:

SimpleForm.setup do |config|
  # ...
  config.input_class = 'foo'
  #...
end

回答by Jaime Mateos

You can setup this in simple_form initializer:

您可以在 simple_form 初始值设定项中进行设置:

config.input_class = 'foo'

config.input_class = 'foo'

It works for me :)

这个对我有用 :)