1

我有一个 html 选择语法,例如:

<select id="salary_structure_salary_structure_line_items_attributes_0_payhead_id" class="half" name="salary_structure[salary_structure_line_items_attributes][0][payhead_id]">
<option data-payhead="Earnings" value="2"> Basic </option>
<option data-payhead="Earnings" value="3"> Daily allowance </option>
<option data-payhead="Standard deductions" value="4"> Employee provident fund </option>
</select>

我如何在 Rails 语法中完成这项工作?我应该在下面的代码中做些什么来得到上面的结果:

<%= select_tag "salary_structure[salary_structure_line_items_attributes][#{index}][payhead_id]", options_from_collection_for_select(@payheads, :id, :payhead_name, :selected => salary_structure_line_item.payhead_id ), :include_blank => true, :class => "half"%> 

我也想在这段代码中使用“选定”选项。

4

2 回答 2

2

嘿,我认为使用集合中的选项进行选择这相当复杂

见:http ://www.redguava.com.au/2011/03/rails-3-select-list-items-with-custom-attributes/

于 2012-10-27T05:28:43.540 回答
1
options_array = [
  ["Basic", 2, "data-payhead" => "Earnings"],
  ["Daily allowance", 3, "data-payhead" => "Earnings"],
  ["Standard deductions", 4, "data-payhead" => "Standard deductions"]
]

select_tag("salary_structure[salary_structure_line_items_attributes][0][payhead_id]", options_for_select(options_array, value), :class => "half")

在上面的代码中,请在 options_for_select 方法中使用选择框选项的值指定值。这将嵌入该选项的选定属性

于 2012-10-27T07:07:45.420 回答