本文介绍了使用SimpleForm向每个选择选项添加一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SimpleForm从数组(而不是模型的集合)构建select输入,并且每个option都有不同的类.

I want to build a select input from an array (and not a collection of models), with SimpleForm, and have different classes for each options.

我希望这会起作用:

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>

如何用简单的形式做我想做的事情(值应该是选项值")?

How can I do what I want (value should be "option value") with simple form?

推荐答案

使用集合时,这似乎是一个限制,请参见SimpleForm的解释作者此处.他建议采用以下解决方法:

This appears to be a limitation when using collections, see the author of SimpleForm's explanation here. He recommends a workaround of the form:

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

这篇关于使用SimpleForm向每个选择选项添加一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 03:07