15
votes

I'm starting with mongoid and simple_form. If I have a model with :type => Boolean and render it in a (haml) view with =f.input, it creates a text input field not a checkbox.

The doc says that boolean maps to a checkbox but I suspect that Boolean and boolean are not handled the same.

I can work around with adding :as => boolean to the simple_form.

1) Is that expected or am I doing something wrong? 2) Is there a way to add a general mapping of Boolean to simple_form? 3) Should I generate an issue for simple_form to correct this?

Thanks in advance.

2
I disagree with closing this question. This is a real problem. simple_form f.input cannot handle a boolean value and render the expected checkbox. Simple_form renders a text field instead (with true or false value). Quite annoying (especially with a name like simple_form ;-(. If you use rails standard form helpers it works just fine.Rutger Karlsson
Since this has been closed for some reason. Here is what I did. Hope it helps. <%= f.input :active, :label => false, :inline_label => "Active?" ,:as => :boolean %>Rutger Karlsson
I think perhaps the moderator closed it because they did not notice that Boolean (the object) is not the same as boolean (the primitive) and therefore thought it made no sense. Maybe?Scott Eisenberg
This is a real bug and a useful question. Shouldn't be closed.Raphael
@RutgerKarlsson, the moderator opened this question back up, and the answer you posted in the comments worked like a charm. Post it as an actual answer now and get your well-deserved reputation points! :)Raphael

2 Answers

12
votes

Unfortunately, simple_form does not currently work "automatically" with Mongoid. You have to specify the as: :boolean.

There have been indications that people want Mongoid integration, but I don't think that it has happened yet. So, why not give it a shot? Here are some relevant tickets from the issue tracker:

0
votes

As David mentioned Mongoid is not supported by SimpleForm at this moment but you can quickly patch Mongoid::Document to make them play nicely like so:

module Mongoid
  module Document
    ARFakeColumn = Struct.new(:type, :limit, :number?)

    def column_for_attribute(attribute_name)
      attribute_name = attribute_name.to_s
      column_name = aliased_fields[attribute_name] || attribute_name
      if fields[column_name] && fields[column_name].options[:type] == Mongoid::Boolean
        ARFakeColumn.new(:boolean, 100)
      else
        ARFakeColumn.new(:string, 100)
      end
    end
  end
end