2013-10-29 68 views
2

我在簡單的表單做了一個表格此行保存值:的Rails不能從單選按鈕

<fieldset> 
    <legend>Any acessory?</legend> 
     <li><%= f.input :has_acessory, label: false, collection: ["0","1"], as: :radio_buttons, input_html:{ name: 'has_acessory'} %></li> 
</fieldset> 

和簡單的表單生成驗證碼:

<fieldset> 
    <legend>Any Acessory?</legend> 
     <li><div class="input radio_buttons optional lending_has_acessory"><label class="radio"><input checked="checked" class="radio_buttons optional" id="lending_has_acessory_0" name="has_acessory" type="radio" value="0" />0</label><label class="radio"><input class="radio_buttons optional" id="lending_has_acessory_1" name="has_acessory" type="radio" value="1" />1</label></div></li> 
</fieldset> 

我使用的SQLite3 ,這裏有一個名爲lendings的表,其中有一個名爲has_acessory的整數列,它的默認值是:0

In Lending model:

class Lending < ActiveRecord::Base 
attr_accessible :devolution_date, :lending_date, :situation, :description,:has_acessory, :person_id, :equipment_id 
#Associations 
belongs_to :equipment 
belongs_to :person 

但它並不重要的價值,我的單選按鈕選擇,我總是得到「0」(默認值)column.I已經檢查了PARAMS的has_acessory,和我可以找到「has_acessory」=>「0」或「1」。

對我來說這部分工作得很好,但爲什麼不能將它保存在has_acessory列中?

回答

0

我會盡量讓導軌爲你做更多的工作。

請嘗試只是<%= f.input :has_acessory %>,看看有什麼給你。

擁有所有這些<%= f.input :has_acessory, label: false, collection: ["0","1"], as: :radio_buttons, input_html:{ name: 'has_acessory'} %>使它很難調試。

請看遷移/數據庫,並確保該字段是布爾值。如果是這樣,鐵軌會爲你找出所有正確的選擇。

此外 - 你說該字段有一個默認值 - 但我沒有看到在模型中。你是否在模型中忽略了這條線?它是否在rails模型和數據庫中?

+0

當我嘗試只是<%= f.input:has_acessory%>,我可以發現我的問題是在input_html:{name:'has_acessory'}。當我刪除這條指令時,簡單的表單生成name =「lending [has_acessory]「,它工作。感謝你們! –

0

試試這個

添加此行到控制器方法

params[:has_acessory] = params[:has_acessory].to_i