我正在使用这些 gem 版本:
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"
and "true"
。
当我启动控制台时,我可以看到没有注入布尔值,而是字符串:
2.0.0p247 :001 > User.last
=> #<User _id: 5283c38563687282c5000000, email: "example@gmail.com", tos_agreement: "1">
我能做些什么来插入一个布尔值?