add checkbox with simple_form without association with model?

后端 未结 6 1459
北荒
北荒 2020-12-29 03:49

How I can add checkbox with simple_form without association with model? I want to create checkbox which will handle some javascript events, but don\'t know? Maybe I miss som

6条回答
  •  一个人的身影
    2020-12-29 04:16

    Add this to app/inputs/arbitrary_boolean_input.rb:

    class ArbitraryBooleanInput < SimpleForm::Inputs::BooleanInput
      def input(wrapper_options = nil)
        tag_name = "#{@builder.object_name}[#{attribute_name}]"
        template.check_box_tag(tag_name, options['value'] || 1, options['checked'], options)
      end
    end
    

    then use it in your views like:

    = simple_form_for(@some_object, remote: true, method: :put) do |f|
      = f.simple_fields_for @some_object.some_nested_object do |nested_f|
        = nested_f.input :some_param, as: :arbitrary_boolean
    

    i.e. The above implementation supports nested fields correctly. Other solutions I've seen do not.

    Note: This example is HAML.

提交回复
热议问题