31

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 something in documentation? Want't to use similar like following:

= simple_form_for(resource, as: resource_name, url: session_url(resource_name), wrapper: :inline) do |f|
  .inputs
    = f.input :email, required: false, autofocus: true
    = f.input :password, required: false
    = f.input :remember_me, as: :boolean if devise_mapping.rememberable?
    = my_checkbox, 'some text'
Mikhail Grishko
  • 4,258
  • 3
  • 22
  • 21
  • if this checkbox haven't association with model why dont use standart checkbox helper? http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-check_box_tag – Mikhail Nikalyukin Feb 07 '12 at 19:21
  • I dont think you can use simple_form helpers without record field. Jus t use simple_form generated classes for your checkbox and you dont need to add custom css. Im notice that you are from Sevastopol come in to our local Sevastopol.rb meetup in this Thusday! Cheers! – Mikhail Nikalyukin Feb 07 '12 at 21:21
  • Thank's, I looking about all changes in that group. But weather not corresponding to nice walking, maybe in Marth) – Mikhail Grishko Feb 08 '12 at 06:45

6 Answers6

36

You can add a custom attribute to the model:

class Resource < ActiveRecord::Base
  attr_accessor :custom_field
end

Then use this field as block:

= f.input :custom_field, :label => false do 
  = check_box_tag :some_name

Try to find "Wrapping Rails Form Helpers" in their documentation https://github.com/plataformatec/simple_form

Gacha
  • 1,037
  • 8
  • 18
35

You could use

f.input :field_name, as: :boolean
huoxito
  • 560
  • 6
  • 7
  • 17
    please notice `without association with model` if the `field_name` is not defined in the model it wont work – Muntasim Jun 30 '14 at 05:26
18

The command proposed by huoxito does not work (at least not in Rails 4). As far as I figured out the error is caused by Rails trying to look up the default value for :custom_field, but because this field does not exists this lookup fails and an exception is thrown.

However, it works if you specify a default value for the field using the :input_html parameter, e.g. like this:

= f.input :custom_field, :as => :boolean, :input_html => { :checked => "checked" }
m4r73n
  • 756
  • 13
  • 21
3

This question comes first on google with no appropriate answer.

Since Simple Form 3.1.0.rc1 there is a proper way of doing it explained on the wiki: https://github.com/plataformatec/simple_form/wiki/Create-a-fake-input-that-does-NOT-read-attributes

app/inputs/fake_input.rb:

class FakeInput < SimpleForm::Inputs::StringInput
  # This method only create a basic input without reading any value from object
  def input(wrapper_options = nil)
    merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
    template.text_field_tag(attribute_name, nil, merged_input_options)
  end
end

Then you can do <%= f.input :thing, as: :fake %>

For this specific question you have to change the second line of the method to:

template.check_box_tag(attribute_name, nil, merged_input_options)

For versions prior 3.1.0.rc1 admgc gave a solution that is adding the missing method merge_wrapper_options:

https://stackoverflow.com/a/26331237/2055246

Community
  • 1
  • 1
eXa
  • 618
  • 8
  • 18
3

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.

Benjamin Dobell
  • 4,042
  • 1
  • 32
  • 44
1

Here is another variation:

= f.label :some_param, class: "label__class" do
  = f.input_field :some_param, class: "checkbox__class"
  Label text
Anton S.
  • 969
  • 1
  • 11
  • 29