Reputation: 4787
I am building a daily deal app to train learning RoR.
On my Deal form, I have a boolean field called "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)
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?
Upvotes: 4
Views: 3622
Reputation: 4820
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. 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:
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.
Upvotes: 1