我有一张表格
<%= form_for @customer do |f| %>
<div class="row">
<div class="col-md-4 col-sm-4 col-xs-4">
<%= f.text_field :name, placeholder: "Customer Name", class: 'form-control'%>
</div>
</div>
<br/>
<table>
<thead>
<tr>
<td>Address1</td>
<td>Address2</td>
<td>City</td>
<td>State</td>
<td>Zip</td>
<td>Delete</td>
</tr>
</thead>
<%= f.fields_for :customer_addresses do |builder| %>
<%= render 'customer_addresses/table_form', f: builder %>
<% end %>
</table>
<%= link_to_add_fields 'Add Field', f, :customer_addresses %>
<br/><br/>
<%= f.submit class: "btn btn-default" %>
使用嵌套表单(customer_addresses / table_form)
<tr>
<td><%= f.text_field :address1, class: 'form-control', placeholder: 'Address 1' %></td>
<td><%= f.text_field :address2, class: 'form-control', placeholder: 'Address 2' %></td>
<td><%= f.text_field :city, class: 'form-control', placeholder: 'City' %></td>
<td><%= f.select :state, options_for_select(us_states), class: 'form-control' %></td>
<td><%= f.text_field :zip, class: 'form-control', placeholder: 'Zip' %></td>
<td><%= f.hidden_field :_destroy %><%= link_to 'remove', '#', class: 'remove_fields' %></td>
</tr>
并且所有作品都很棒并且很好地渲染了表单。但是当select
字段的:state
在编辑窗口中呈现时,它始终是Alabama(us_states
辅助函数导出的第一个状态。)我将添加到{{{ 1}}以便使用所选选项呈现客户地址中的选定状态?
答案 0 :(得分:2)
没有理由将options_for_select
与select
一起使用(仅限select_tag
),因为select
如果您只是传递相同的参数,那么就会为您做到这一点正在传递给options_for_select
。
所以,你必须做类似的事情:
f.select :state, us_states #, etc
我假设:state
不是关联,如果是,那么它应该是:state_id