我有一个表单,我尝试在其中自动聚焦于 collection_select,但这不起作用。它适用于其他表单项,例如 number_field 和 text_field。我不明白为什么?
正在运行的代码:
<tr>
<td><%= form.label :invoice_number %></td>
<td><%= form.number_field :invoice_number, value: 100, autofocus: true %></td>
</tr>
不工作的代码:
<tr>
<td><%= form.label :customer_id %></td>
<td><%= form.collection_select :customer_id, Customer.all, :id, :name, autofocus: true %></td>
</tr>
请您参考如下方法:
number_field
只有一个 options
哈希参数:number_field(object_name, method, options = {})
collection_select
有两个:collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
因此,您需要这样调用它:
<%= form.collection_select :customer_id, Customer.all, :id, :name, {}, {autofocus: true} %>