2

我用简单的形式制作了这一行:

 <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>

和 Simple Form 生成以下代码:

<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,其中有一个名为借贷的表,其中有一个名为 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

结尾

但是我在单选按钮中选择哪个值并不重要,我总是在 has_acessory 列中得到“0”(默认值)。我已经检查了参数,我可以找到“has_acessory”=>“0”或“1”。

对我来说,这部分工作得很好,但为什么不能保存在 has_acessory 列中?

4

2 回答 2

0

尝试这个

将此行添加到控制器方法中

params[:has_acessory] = params[:has_acessory].to_i
于 2013-10-29T13:34:25.863 回答
0

我会尽量让 Rails 为你做更多的工作。

请尝试一下<%= f.input :has_acessory %>,看看它给了你什么。

拥有这一切<%= f.input :has_acessory, label: false, collection: ["0","1"], as: :radio_buttons, input_html:{ name: 'has_acessory'} %>使得调试变得困难。

请查看迁移/数据库并确保该字段为布尔值。如果是这样,rails 将为您找出所有正确的选项。

另外-您说该字段具有默认值-但我在模型中看不到。你只是在模型中省略了那条线吗?它在rails模型和数据库中吗?

于 2013-10-29T13:34:29.690 回答