Ruby on Rails – Checkbox没有保存到数据库?
我有一个使用布尔值的迁移,并在其视图中生成一个复选框。 但是,无论我单击什么,保存到数据库的值都不会受到影响。
我的迁移看起来像这样:
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
我的观点如下:
哪个产生以下HTML:
控制器只是脚手架生成的默认值。
如果我在迁移中设置了默认值,则该值始终保存在数据库中。 如果我没有设置默认值,则它始终为NULL。
任何人都可以提出解决方案,建议可能出现的问题?
任何建议表示赞赏
谢谢。
Doh,我忘了在模型中设置attr_accessible。
尝试使用form_for
而不是semantic_form_for
,并将<%= form.input :allow_comments %>
替换为<%= form.check_box_field :allow_comments %>