4

SimpleForm を使用して (モデルのコレクションではなく) 配列から入力を作成し、各sselectに異なるクラスを持たせたいと考えています。option

私はこれがうまくいくことを望んでいたでしょう:

f.input :method, collection: [
    ["option text", "option_value_1", { class: "class_name_1" }],
    ["option text 2", "option_value_2", { class: "class_name_2" }]
]

それに関する問題は、次のものが生成されることです。

<select>
    <option value="option text" class="class_name_1">option text</option>
    <option value="option text 2" class="class_name_2">option text 2</option>
</select>

単純なフォームで必要なこと (値は「オプション値」にする必要があります) を行うにはどうすればよいですか?

4

2 に答える 2

8

これは、コレクションを使用する場合の制限のようです。SimpleForm の説明の作成者はこちらを参照してください。彼は、次の形式の回避策を推奨しています。

f.input :method, :as => :select do
  f.select :method, [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
end
于 2013-03-03T21:31:26.597 に答える
0

配列の配列を引数として渡すこともできます

= f.input :status, collection: [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
于 2019-05-08T13:18:42.100 に答える