Ruby on Rails的 - 复选框不保存到数据库? [英] Ruby on Rails - Checkbox not saving to database?
问题描述
我已经得到它使用一个布尔值,并生成它认为一个复选框迁移。然而,无论怎样我点击,保存到数据库中的值不会受到影响。
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
我的看法是这样的:
My view looks like this:
<% 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 %>
&LT;%结束%>
<% end %>
这将产生以下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?
任何意见AP preciated。
Any advice appreciated.
感谢。
推荐答案
卫生署,我忘了设定attr_accessible在模型中。
Doh, I'd forgotten to set attr_accessible in the model.
这篇关于Ruby on Rails的 - 复选框不保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!