【发布时间】:2012-03-14 06:58:35
【问题描述】:
我有一个模型 (CustInfo),它包含 3 个属性:cust_name、年龄、性别。在我的编辑视图中,我使用 form_for 进行表单提交:
<%= form_for @cust[0] do |cust| %>
<label for="cname">Customer name: </label>
<%= cust.text_field :cust_name, :size => 20 %>
<label for="age">Customer age: </label>
<%= cust.text_field :age, :size => 5 %>
<label for="gender">Gender </label>
<%= radio_button_tag(:gender, "F", :checked => (:gender == 'female')? true:false) %><span style="float:left">F</span>
<%= radio_button_tag(:gender, "M", :checked => (:gender == 'male')? true:false) %><span style="float:left">M</span>
<%= cust.submit "Save" %>
<% end %>
在我的控制器中
def update
if Cust.update_all(params[:custinfo])
flash[:notice] = "Successfully updated!"
redirect_to :action => "index"
else
flash[:notice] = "There are errors in the form, please try again"
render :action => "edit"
end
end
当我按下提交按钮时,表单提交,其参数如下:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"I9QEdP1mRr65wT9TRbzNokkaa+LHVyPfmgWJMKoup", "custinfo"=>{"age"=>"16", "cust_name"=>"jsmith"}, "gender"=>"M","commit"=>"Save"}.
看起来性别确实通过 params 传递,但它不在 params[:model] 中,在本例中为 params[:custinfo],这就是更新函数不会更新这个值的原因(因为我只有 Cust .update_all(params[:custinfo]))。我的问题是为什么只有这个单选按钮不在 params[:custinfo] 中,而其他两个在?以及如何在 params[:custinfo] 中传递单选按钮值?谢谢!
【问题讨论】: