LoginSignup
24
22

More than 5 years have passed since last update.

ラジオボタンの項目数が多すぎてもsimple_formでそこそこすっきり書く方法

Last updated at Posted at 2012-09-14

ラジオボタンをsimple_formで表現するときは以下のような感じで書くと思います。

app/views/surveys/new.html.haml
simple_form_for @survey do |f|
  - fav_dog_list = [["ラブラドールレトリバー", :a1], ["ゴールデンレトリバー", :a2], ["スタンダードプードル", :a3]
  f.input :fav_dog, as: :radio_buttons, collections: fav_dog_list
  # ...
end

これだと国際化対応するときに面倒なことになるし、しなくてもビューが荒れるので、こんな感じで分けて書くようになると思います。

config/locales/simple_form_ja.yml
ja:
  simple_form:
    options:
      survey:
        fav_dog:
          a1: "ラブラドールレトリバー"
          a2: "ゴールデンレトリバー"
          a3: "スタンダードプードル"
app/views/surveys/new.html.haml
simple_form_for @survey do |f|
  - fav_dog_list = [:a1, :a2, :a3]
  f.input :fav_dog, as: :radio_buttons, collections: fav_dog_list
  # ...
end

多少スッキリしましたが、simple_form_ja.ymlにも選択項目の値が書いてあるのに、ビュー側にも書いてあるのは何だかDRYじゃない気がします。

そこで以下のようなモンキーパッチを施します。

config/initializer/simple_form_expand.rb
# -*- coding: utf-8 -*-
module SimpleForm
  module Inputs
    class CollectionRadioButtonsInput < SimpleForm::Inputs::CollectionInput
      def input
        _collection = collection
        # translateが登録されている場合は、項目値としてtranslateのキー値を使用する
        if translate(:options).present?
          _collection = translate(:options).to_a.map { |elem| [elem.last, elem.first] }
        end
        label_method, value_method = detect_collection_methods

        @builder.send("collection_#{input_type}",
          attribute_name, _collection, value_method, label_method,
          input_options, input_html_options, &collection_block_for_nested_boolean_style
        )
      end
    end
  end
end

するとビュー側では以下のように書けるようになります。

app/views/surveys/new.html.haml
simple_form_for @survey do |f|
  f.input :fav_dog, as: :radio_buttons
  # ...
end

ラジオボタンが100個ぐらいある入力システムを作っているときは便利ですね!

24
22
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
22