Question

I have the following in my application:

# 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

But the boolean model attribute does not get updated. No error is thrown either. Any idea why my application don't convert the "true" or "false" string to a boolean value.

I have done it like shown in the documentation. But it seems like Rails 4 is generally by default not as open to receive boolean values from view/controller, like like exemplified in this SO post.

The server log says:

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
Was it helpful?

Solution

You can get around your current issue by doing this ...

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

Or if the params value was '1' as the best_in_place docs says then it would be

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

Also this gem looks like it would work nicely for this as well https://github.com/prodis/wannabe_bool

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top