简体   繁体   English

活动管理字段上的布尔值返回空而不是true或false(Rails 3.2 / Active Admin)

[英]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.

相关问题 活动管理字段上的布尔值返回空而不是false(Rails 3.2 / Active Admin) - Boolean on active admin field returns empty instead false (Rails 3.2/Active Admin) 下拉字段的经济行业/行业列表(rails 3.2,活动管理员) - List of economic industries/sectors for a dropdown field (rails 3.2, active admin) 在Active Admin上进行多选过滤(Rails 3.2 / postgreSQL / active admin 1.0) - Filter on multi-select on Active Admin (Rails 3.2/postgreSQL/active admin 1.0) Ruby:如何根据变量数量在 Active Admin 面板中对单词进行复数/单数化(Active Admin、rails 3.2、ruby) - Ruby: How to pluralize/singularize a word in Active Admin panel according to variable quantity (Active Admin, rails 3.2, ruby) 根据Active Admin控制台中的一个Category属性计算数量(Rails 3.2 / active-admin 1.0) - Calculate quantity based on one Category attribute in Active Admin Dashboard (Rails 3.2/ active-admin 1.0) 在Active Admin(Rails 3.2,Active Admin 1.0)的依存选择下拉列表中找不到错误 - Can't find the error in my dependent select drop down on Active Admin( Rails 3.2, Active Admin 1.0) 如何在Active Admin User页面上显示用户总数(rails 3.2,active admin gem) - How to display total number of users on Active Admin User page (rails 3.2, active admin gem) Rails 3.2 Active Admin,登录时更改密码 - Rails 3.2 Active Admin, Change password while logged in 筛选:字符串无法在Active Admin上运行(Rails 3.2 / postgresql) - Filter on :string not working on Active Admin (Rails 3.2/postgresql) Rails 4活动管理员问题 - Rails 4 Active Admin Problems
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM