我有一個使用嵌套形式創建了以下選擇框:Rails 3:爲什麼選擇框不能傳遞正確的值?
<select name="product[shop_attributes][id]" id="product_shop_attributes_id">
<option value="23">KMART</option>
<option value="24">Super Shop</option>
<option selected="selected" value="22">TARGET</option>
<option value="new">Create New Shop</option>
</select>
selected="selected"
被傳遞到:selected => "22"
選項f.select
創建。
問題是無論選擇什麼選項,提交的值始終爲"22"
。
我注意到,一個隱藏的輸入被創建,它相信引起該問題:
<input type="hidden" value="22" name="product[shop_attributes][id]" id="product_shop_attributes_id">
因此,存在2個元素id=product_shop_attributes_id
。
什麼可能導致這個隱藏的輸入字段被生成?
選擇框創建的相關代碼:
<%= form_for @product do |f| %>
<%= f.fields_for :shop do |sf| %>
sf.select(:id, <options>, {:prompt => true, :selected => <default_value>})
<% end %>
<% end %>
相關的控制器代碼:
def edit
@product = Product.find(params[:id]) # the select box indeed gets it's initial value from @product
end
def update
@temp = params.inspect
end
update.html.erb:
<%= @temp %>
我總是在這裏看到的一樣(不管是什麼選項i被選中):
"product"=>{"shop_attributes"=>{"id"=>"22"},...}
您不需要傳遞:選中 - 您正在使用模型表單。你只需要用select_tag。 – 2010-12-20 06:12:41
我同意,謝謝! – 2010-12-20 09:47:03