[英]Boolean on active admin field returns empty instead of true or false (Rails 3.2/Active Admin)
I am building a daily deal app to train learning RoR. 我正在建立一个日常交易应用程序来培训学习RoR。
On my Deal form, I have a boolean field called "featured". 在我的Deal表单中,我有一个名为“featured”的布尔字段。 If I check the checkbox, the deal is featured (as opposed to a draft).
如果我勾选复选框,则会显示交易(而不是草稿)。
But when I create on active admin my Deal, if I check the checkbox, I do get 'true' (that part is ok), but if I don't check it, I am getting 'empty' instead of 'false' . 但是当我在主动管理我的交易创建时,如果我选中复选框,我确实得到'真'(那部分没问题),但是如果我不检查它, 我得到'空'而不是'假' 。
Shouldn't I get false? 我不应该弄错吗?
Here are my files: 这是我的文件:
schema migration: 架构迁移:
create_table "deals", :force => true do |t|
t.string "title"
t.string "description"
t.boolean "featured"
t.integer "admin_user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
And the form on Active Admin (it uses formtastic for forms by default I think) 和Active Admin上的表单(我认为它默认使用formtastic for forms)
ActiveAdmin.register Deal do
controller do
with_role :admin_user
end
form do |f|
f.inputs "Content" do
f.input :description, :label => "Deal description"
f.input :title, :label => "Deal title"
end
f.inputs "Status" do
f.input :featured, :label => "Status of publication (draft or featured)"
end
f.inputs "Publisher" do
f.input :admin_user_id, :as => :select, :collection => AdminUser.all, :label => "Campaign Account Manager"
end
f.actions
end
end
Anybody has an idea why in the column "featured" I can read "empty" instead of "false" when I don't check the checkbox of the "featured" field when creating Deals? 任何人都知道为什么在“特色”栏中,当我在创建交易时没有选中“特色”字段的复选框时,我可以读取“空”而不是“假”?
I am assuming that by 'empty', you don't mean the literal but you mean the field has no value or is empty. 我假设通过'空',你不是指字面意思,但你的意思是字段没有价值或是空的。 You haven't set a default for the field or entered any data into it, so it is empty or, in the Ruby vernacular, nil.
您没有为字段设置默认值或在其中输入任何数据,因此它是空的,或者在Ruby白话中,为零。 To set a default, you can do something like this:
要设置默认值,您可以执行以下操作:
create_table "deals", :force => true do |t|
t.string "title"
t.string "description"
t.boolean "featured" :default => false
t.integer "admin_user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
There are other methods for setting defaults as well, for more complex values. 对于更复杂的值,还有其他设置默认值的方法。 For example, if you wanted to set the default for a datetime field to the current time, you would use a before_create exit:
例如,如果要将datetime字段的默认值设置为当前时间,则可以使用before_create出口:
before_create :set_foo_to_now
def set_foo_to_now
self.foo = Time.now
end
Or, you can simply make sure you enter a value when you create the new record yourself. 或者,您可以确保在自己创建新记录时输入值。
As a reference, see this text on ActiveRecord migrations . 作为参考,请参阅有关ActiveRecord迁移的本文 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.