Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add checkbox with simple_form without association with model?

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'
like image 562
Mikhail Grishko Avatar asked Feb 07 '12 19:02

Mikhail Grishko


3 Answers

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

like image 152
Gacha Avatar answered Sep 18 '22 14:09

Gacha


You could use

f.input :field_name, as: :boolean 
like image 41
huoxito Avatar answered Sep 20 '22 14:09

huoxito


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" }
like image 45
m4r73n Avatar answered Sep 19 '22 14:09

m4r73n