Stack Stack
Stack Stack

Reputation: 147

I want to use bootstrap checkboxes

I want to use Bootstrap checkboxes.

<div class="btn-group" data-toggle="buttons-checkbox">
  <button type="button" class="btn btn-primary">Left</button>
  <button type="button" class="btn btn-primary">Middle</button>
  <button type="button" class="btn btn-primary">Right</button>
</div>

How do I integrate it into a Rails application?

I would like to use them in a form. Specifically, how can I send and catch the selected data?

UPDATE:

I know how to use checkboxes in Rails. I don't understand how can I set their style via bootstrap.

Checkbox is - <input type="checkbox">Simple</input>

Bootstrap checkbox is <button type="button" class="btn btn-primary">Bootstrap</button>

UPDATE 2:

As example, see my question about radio buttons.

Upvotes: 0

Views: 1769

Answers (2)

rudolph9
rudolph9

Reputation: 8119

Consider using bootstrap-rails to incorporate to bootstrap css and javascript libraries into you application. There are several flavors of rails bootstrap but I personally use this one because it is written in SASS (more commonly used with rails than LESS).

You have some issues with how you approaching this as its seems you have three options and only one of which I'd assume should be selected (for which I recommend using a select tag) and you are using buttons not checkboxes... but to integrate checkboxes into you application, rails indeed offers a methods, an example in a form_for is as follows:

- form_for @something_with_a_postion do |something|     
  = something.label 'Left'
  = something.check_box 'position', 'left', {class: 'whatever_classes_for_bootstrap_needed'}
  = something.label 'Middle'
  = something.check_box 'position', 'middle', {class: 'whatever_classes_for_bootstrap_needed'}
  = something.label 'Right'
  = something.check_box 'position', 'right', {class: 'whatever_classes_for_bootstrap_needed'}

You can find more on form_for and check_box here.

I also refer you to this question. As well, here is a RailsCast pertaining to checkboxes.

Upvotes: 1

TopperH
TopperH

Reputation: 2223

For example you can delete an item like this:

<p>Delete item <%= asset.check_box :_destroy %></p>

Or it's even easier if you use the simple_form gem, for example if you have a boolean attribute in your model checkboxes are the default:

<%= f.input :agreement %>

Or you can force checkboxes like this:

<%= f.foobar, :as => :check_boxes %>

Upvotes: 4

Related Questions