我正在建立一个日常交易应用程序来训练学习RoR。
在我的Deal表单中,我有一个名为“featured”的布尔字段。 如果我选中复选框,则会显示交易(而不是草稿)。
但是当我在主动管理我的交易创建时,如果我选中复选框,我确实得到'真'(那部分没问题),但是如果我不检查它, 我得到'空'而不是'假' 。
我不应该弄错吗?
这是我的文件:
架构迁移:
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和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任何人都知道为什么在“特色”栏中,当我在创建交易时没有选中“特色”字段的复选框时,我可以读取“空”而不是“假”?
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 endAnd 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 endAnybody 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?
最满意答案
我假设通过'空',你不是指字面意思,但你的意思是字段没有价值或是空的。 您没有为字段设置默认值或在其中输入任何数据,因此它是空的,或者在Ruby白话中,为零。 要设置默认值,您可以执行以下操作:
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对于更复杂的值,还有其他设置默认值的方法。 例如,如果要将datetime字段的默认值设置为当前时间,则可以使用before_create出口:
before_create :set_foo_to_now def set_foo_to_now self.foo = Time.now end或者,您可以确保在自己创建新记录时输入值。
作为参考,请参阅有关ActiveRecord迁移的本文 。
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 => falseThere 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 endOr, 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.
更多推荐
发布评论