チェックボックスとラジオボタンを順序付けられていないリストで一緒にラップする可能性はありますか?
そうでない場合、どうすれば縦に表示できますか?
これはレイアウトに関連していることは知っていますが、それでもプログラミングの問題です。
チェックボックスとラジオボタンを順序付けられていないリストで一緒にラップする可能性はありますか?
そうでない場合、どうすれば縦に表示できますか?
これはレイアウトに関連していることは知っていますが、それでもプログラミングの問題です。
次のように入力を再定義するのは、よりクリーンで優れた方法です。
新しいディレクトリ「app/inputs」を作成し、
その中にファイル「collection_radio_buttons_input.rb」を作成し、以下を貼り付けます
class CollectionRadioButtonsInput < SimpleForm::Inputs::CollectionRadioButtonsInput
  def item_wrapper_class
    "radiobox"
  end
  def build_nested_boolean_style_item_tag(collection_builder)
    collection_builder.radio_button + template.content_tag(:span,collection_builder.text)
  end
end
config/initializers/simple_form.rb でオプション「config.inputs_discovery」をオンにします
出来上がり!
これで、このコントロールがデフォルトの RadioButtons simple_form コントロールの代わりに使用され、任意のフォーマットを自由に使用できるようになります。
生活を続けるための簡単なハックとして、これをアプリケーション ヘルパーの下部に追加すると、各ラベル/入力ペアを div で簡単にラップできます。
module SimpleForm::ActionViewExtensions::Builder
  def collection_radio(attribute, collection, value_method, text_method, options={}, html_options={})
    collection.map do |item|
      value = item.send value_method
      text  = item.send text_method
      default_html_options = default_html_options_for_collection(item, value, options, html_options)
      @template.content_tag(:div, radio_button(attribute, value, default_html_options) <<
        label("#{attribute}_#{value}", text, :class => "collection_radio"))
    end.join.html_safe
  end
  def collection_check_boxes(attribute, collection, value_method, text_method, options={}, html_options={})
    collection.map do |item|
      value = item.send value_method
      text  = item.send text_method
      default_html_options = default_html_options_for_collection(item, value, options, html_options)
      default_html_options[:multiple] = true
      @template.content_tag(:div, 
      check_box(attribute, default_html_options, value, '') <<
        label("#{attribute}_#{value}", text, :class => "collection_check_boxes"))
    end.join.html_safe
  end
end