使用Rails 4的best_in_place gem复选框问题(为什么字符串没有转换为boolean?)

Chr*_*sen 6 boolean ruby-on-rails best-in-place ruby-on-rails-4

我的申请中有以下内容:

# some_view_file.html.haml
= best_in_place element, :inbox, :type => :checkbox

# imap_accounts_controller.rb
def update
  element = ImapAccount.find(params[:id])
  element.update_attributes(params[:imap_account])
  respond_with element
end
Run Code Online (Sandbox Code Playgroud)

但布尔模型属性不会更新.也没有抛出错误.知道我的应用程序为什么不将"true""false"字符串转换为布尔值.

我已经按照文档中的说明完成了它.但是看起来Rails 4通常默认情况下不会打开来接收来自视图/控制器的布尔值,就像在这篇SO帖子中举例说明的那样.

服务器日志说:

Started PUT "/en/imap_accounts/525188ea83c336a4eb000002" for 127.0.0.1 at 2014-01-12 16:43:22 +0100
Processing by ImapAccountsController#update as JSON
  Parameters: {"imap_account"=>{"enable_ssl"=>"false"}, "authenticity_token"=>"mX+Dpghb8nB49qhFTLbGSB2w3pJQg56PBgg8jR7G3/Y=", "locale"=>"da", "id"=>"525188ea83c336a4eb000002"}
  MOPED: 127.0.0.1:27017 QUERY        database=myapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('518f599683c336fb87000003')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.8680ms
  MOPED: 127.0.0.1:27017 QUERY        database=myapp_development collection=imap_accounts selector={"_id"=>BSON::ObjectId('525188ea83c336a4eb000002')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.4400ms
  MOPED: 127.0.0.1:27017 QUERY        database=myapp_development collection=imap_accounts selector={"_id"=>BSON::ObjectId('525188ea83c336a4eb000002')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.6020ms
  MOPED: 127.0.0.1:27017 QUERY        database=myapp_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('518f599683c336fb87000003')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.3010ms
  MOPED: 127.0.0.1:27017 QUERY        database=myapp_development collection=imap_accounts selector={"_id"=>BSON::ObjectId('525188ea83c336a4eb000002')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.5740ms
Completed 204 No Content in 182ms
Run Code Online (Sandbox Code Playgroud)

Mil*_*eld 0

您可以通过这样做来解决当前的问题...

# imap_accounts_controller.rb
def update
  element = ImapAccount.find(params[:id])
  element.update_attributes(params[:imap_account] == "true")
  respond_with element
end
Run Code Online (Sandbox Code Playgroud)

或者,如果参数值为“1”,如 best_in_place 文档所述,那么它将是

# imap_accounts_controller.rb
def update
  element = ImapAccount.find(params[:id])
  element.update_attributes(params[:imap_account] == "1")
  respond_with element
end
Run Code Online (Sandbox Code Playgroud)

另外,这个 gem 看起来也很适合这个https://github.com/prodis/wannabe_bool