我正在使用这些宝石版本:
gem 'rails', '4.0.1'
gem 'mongoid', '~> 4', github: 'mongoid/mongoid'
gem 'devise', '3.2.0'
我试图用这种形式将布尔选择传递给mongoid:
= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
= f.check_box :tos_agreement
= t(".do_you_accept_our_toc")
我的模型看起来像这样:
class User
include Mongoid::Document
field :tos_agreement, :type => Boolean
服务器日志说明了这一点:
Started POST "/da/users" for 127.0.0.1 at 2013-11-13 19:41:32 +0100
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"nyfk//kJtXXM1MMSWln5fTn4P2vzzeP4XzgC3GTT/tk=", "user"=>{"tos_agreement"=>"1"}, "commit"=>"Create", "locale"=>"en"}
MOPED: 127.0.0.1:27017 QUERY database=mydb_development collection=users selector={"email"=>"example@gmail.com"} flags=[] limit=-1 skip=0 batch_size=nil fields={:_id=>1} runtime: 0.9030ms
如您所见,"1"
已从表单传递,但我也尝试使用"yes"
和"true"
。
当我启动控制台时,我可以看到没有注入布尔值,而是字符串有:
2.0.0p247 :001 > User.last
=> #<User _id: 5283c38563687282c5000000, email: "example@gmail.com", tos_agreement: "1">
如何插入布尔值?
答案 0 :(得分:0)
field :tos_agreement, :type => Mongoid::Boolean