simple_form集合包装器(无线电按钮):项目的双重封装

ale*_*lex 8 simple-form

我想用simple_form重现这个单选按钮的单选按钮,以便使用http://semantic-ui.com/语法使simple_form工作:

  <div class="grouped inline fields">
    <div class="field">
      <div class="ui radio checkbox">
        <input type="radio" name="fruit" checked="">
        <label>Apples</label>
      </div>
    </div>
    <div class="field">
      <div class="ui radio checkbox">
        <input type="radio" name="fruit">
        <label>Oranges</label>
      </div>
    </div>
    <div class="field">
      <div class="ui radio checkbox">
        <input type="radio" name="fruit">
        <label>Pears</label>
      </div>
    </div>
    <div class="field">
      <div class="ui radio checkbox">
        <input type="radio" name="fruit">
        <label>Grapefruit</label>
      </div>
    </div>
  </div>
Run Code Online (Sandbox Code Playgroud)

所以我准备了一个自定义包装器:

config.wrappers :semantic_radios, tag: 'div', class: "grouped fields", error_class:   'error', hint_class: 'with_hint' do |b|
    b.use :html5
    b.use :label
    b.use :input
  end
Run Code Online (Sandbox Code Playgroud)

设置一些选项:

config.item_wrapper_tag = :div
config.item_wrapper_class = 'ui radio checkbox'
Run Code Online (Sandbox Code Playgroud)

并以我的形式调用此代码:

=f.input :child_care_type, collection: [["option 1", 1],["option 2", 2]], as: :radio_buttons, wrapper: :semantic_radios
Run Code Online (Sandbox Code Playgroud)

我不知道在哪里定制div.field封装:

    <div class="field">
      <div class="ui radio checkbox">
        <input type="radio" name="fruit" checked="">
        <label>Apples</label>
      </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

我的代码只呈现这个:

  <div class="ui radio checkbox">
    <input type="radio" name="fruit" checked="">
    <label>Apples</label>
  </div>
Run Code Online (Sandbox Code Playgroud)

你能帮助我吗 ?我没有找到更多包装器的自定义集合:s

adm*_*mgc 6

摘要:

通过创建一个继承SimpleForm::Inputs::CollectionRadioButtonsInput和重载几个方法的自定义输入,我之前做过类似的事情.有关自定义输入组件的更多信息,请参阅https://github.com/plataformatec/simple_form/wiki/Adding-custom-input-components.

在任何情况下,下面的代码几乎完全使用simple_form v2.1.0和rails v3.2.15生成所需的html标记.

码:

# File: app/inputs/semantic_ui_radio_buttons_input.rb

class SemanticUiRadioButtonsInput < SimpleForm::Inputs::CollectionRadioButtonsInput

  # Creates a radio button set for use with Semantic UI

  def input
    label_method, value_method = detect_collection_methods
    iopts = { 
      :checked => 1,
      :item_wrapper_tag => 'div',
      :item_wrapper_class => 'field',
      :collection_wrapper_tag => 'div',
      :collection_wrapper_class => 'grouped inline fields'
     }
    return @builder.send(
      "collection_radio_buttons",
      attribute_name,
      collection,
      value_method,
      label_method,
      iopts,
      input_html_options,
      &collection_block_for_nested_boolean_style
    )
  end # method

  protected

  def build_nested_boolean_style_item_tag(collection_builder)
    tag = String.new
    tag << '<div class="ui radio checkbox">'.html_safe
    tag << collection_builder.radio_button + collection_builder.label
    tag << '</div>'.html_safe
    return tag.html_safe
  end # method

end # class
Run Code Online (Sandbox Code Playgroud)

然后,在您的表单中,只需:

-# File: app/views/<resource>/_form.html.haml

-# Define the collection
- child_care_coll = %w( Infant Toddler Preschool Kindergarten ).map!.with_index(1).to_a

-# Render the radio inputs
= f.input :child_care_type,
  :collection    => child_care_coll,
  :label_method  => :first,
  :value_method  => :last,
  :as            => :semantic_ui_radio_buttons
Run Code Online (Sandbox Code Playgroud)

结果:

<div class="input semantic_ui_radio_buttons optional childcare_child_care_type">

  <label class="semantic_ui_radio_buttons optional control-label">
    Child care type
  </label>

  <div class="grouped inline fields">

    <div class="field">
      <div class="ui radio checkbox">
        <input checked="checked" class="semantic_ui_radio_buttons optional" id="childcare_child_care_type_1" name="childcare[child_care_type]" type="radio" value="1">
        <label for="childcare_child_care_type_1">Infant</label>
      </div>
    </div>

    ...

    <div class="field">
      <div class="ui radio checkbox">
        <input class="semantic_ui_radio_buttons optional" id="childcare_child_care_type_4" name="childcare[child_care_type]" type="radio" value="4">
        <label for="childcare_child_care_type_4">Kindergarten</label>
      </div>
    </div>

  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 6

在我查看 config/initializers/simple_form.rb 之前,我遇到了同样的问题。

结果你可以在这里设置选项(第~51行):

# Define the way to render check boxes / radio buttons with labels.
# Defaults to :nested for bootstrap config.
#   inline: input + label
#   nested: label > input
config.boolean_style = :inline
Run Code Online (Sandbox Code Playgroud)

再往下,您还可以定义集合的默认包装标签和包装标签类(第 81 行):

 # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
 config.collection_wrapper_tag = :div

 # You can define the class to use on all collection wrappers. Defaulting to none.
 config.collection_wrapper_class = 'styled-radios'
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助:)

*使用 gem 'simple_form'