我有一个表单,我尝试在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>
答案 0 :(得分:2)
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} %>