Ruby on Rails - 复选框不保存到数据库? [英] Ruby on Rails - Checkbox not saving to database?

查看:207
本文介绍了Ruby on Rails - 复选框不保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用布尔值的迁移,并在其视图中生成一个复选框。但是,无论我点击什么,保存到数据库的值不受影响。

I've got a migration which uses a boolean value and generates a checkbox in its view. However, no matter what I click, the value saved to the database is not affected.

我的迁移如下所示:

def self.up
    create_table :blogposts do |t|
      t.string :title
      t.text :body
      t.boolean :allow_comments, :default => false  
      t.references :author
      t.references :lasteditor
      t.timestamps
    end
  end

我的视图如下所示:

<% semantic_form_for([:controlpanel, @blogpost]) do |form| %>
<%= form.error_messages %>
<% form.inputs do %>
<%= form.input :title %>
<%= form.input :body %>
<%= form.input :allow_comments %>
<% end %>
<%= form.buttons %>

其中生成以下HTML:

Which produces the following HTML:

<li class="boolean required" id="blogpost_allow_comments_input">
<label for="blogpost_allow_comments">
<input id="blogpost_allow_comments" name="blogpost[allow_comments]" type="checkbox" value="1" />
<input name="blogpost[allow_comments]" type="hidden" value="0" />Allow comments
<abbr title="required">*</abbr>
</label>
</li> 

控制器只是由脚手架生成的默认控制器。

The controller is just the default generated by the scaffold.

如果我在迁移中设置了默认值,那么该值始终保存在数据库中。如果我没有设置默认值,它总是NULL。

If I set the default in the migration, that value is always saved in the database. If I do not set a default, it is always NULL.

任何人都可以提出一个解决方案,建议什么可能会出错?

Can anyone suggest a solution, suggestion on what might be going wrong?

任何建议。

谢谢。

推荐答案

Doh,我忘了在模型中设置attr_accessible。

Doh, I'd forgotten to set attr_accessible in the model.

这篇关于Ruby on Rails - 复选框不保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆