Ruby on rails 轨道可以';t保存单选按钮的值
我用简单的形式写了这行:Ruby on rails 轨道可以';t保存单选按钮的值,ruby-on-rails,sqlite,radio-button,simple-form,Ruby On Rails,Sqlite,Radio Button,Simple Form,我用简单的形式写了这行: <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><%= 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>
有什么特别的吗?
01
我使用的是SQLite3,其中有一个名为lendings的表,其中有一个整数列名为has_acessory,它的默认值是:0
在贷款模式中:
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
classlending
结束
但是我在单选按钮中选择哪个值并不重要,我总是在has_acessory列中得到“0”(默认值)。我已经检查了参数,我可以找到“has_acessory”=>“0”或“1”
对我来说,这部分工作得很好,但是为什么不能保存在has_acessory列中呢?试试这个
将此行添加到控制器方法中
params[:has_acessory] = params[:has_acessory].to_i
我会尽量让rails为您做更多的工作 请尝试一下
,看看这会给你带来什么
拥有所有这些
会使调试变得困难
请查看迁移/数据库,并确保该字段为布尔值。如果是,rails将为您找到所有正确的选项
另外,你说这个字段有一个默认值,但我在模型中没有看到。你是不是在模型中省略了那一行?它在rails模型和数据库中都存在吗?当我尝试时,我发现我的问题出在输入html:{name:'has_acessory'}。当我删除此指令时,simple form生成name=“lending[has_acessory]”并且成功了。谢谢你们!