活动管理字段上的布尔值返回空而不是true或false(Rails 3.2 / Active Admin)(Boolean on active admin field returns empty in

编程入门 行业动态 更新时间:2024-10-26 12:22:19
活动管理字段上的布尔值返回空而不是true或false(Rails 3.2 / Active Admin)(Boolean on active admin field returns empty instead of true or false (Rails 3.2/Active Admin))

我正在建立一个日常交易应用程序来训练学习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 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?

最满意答案

我假设通过'空',你不是指字面意思,但你的意思是字段没有价值或是空的。 您没有为字段设置默认值或在其中输入任何数据,因此它是空的,或者在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 => 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.

更多推荐

本文发布于:2023-07-29 14:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   而不是   布尔值   Rails   false

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!