我有一個模型(CustInfo),它包含3個屬性:cust_name,年齡,性別。在我的編輯視圖中,我使用的form_for的表單提交:在rails中的單選按鈕值form_for不是參數的一部分[:model]
<%= 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 [:model]中,在這種情況下爲params [:custinfo],這就是更新函數不會更新此值的原因(因爲e我只有Cust.update_all(params [:custinfo]))。我的問題是,爲什麼只有這個單選按鈕不是params [:custinfo],而另外兩個是?我怎麼能傳遞params [:custinfo]內的單選按鈕值?謝謝!
你是正確的兩種情況。謝謝! – swingfuture 2012-02-23 21:20:05