dialex
dialex

Reputation: 2866

Using bootstrap-switch with Rails' check_box

I need to display a checkbox for a User's field. Currently I do so using:

<%= f.label :is_name_public, "Public name" %>
<%= f.check_box :is_name_public %>

Now I want to use bootstrap-switch with Rails' chech_box. In order to use the switch button, the checkbox needs to be something like:

<input id="user_is_name_public" name="user[is_name_public]" type="checkbox" value="1" data-size="small" data-on-color="success" data-on-text="Yes" data-off-text="No">

instead of Rails' default:

<input id="user_is_name_public" name="user[is_name_public]" type="checkbox" value="1">

The question is, how to tell Rails to add custom properties like data-size="small" or data-on-color="success" to the checkbox? Or how to associate a custom html checkbox with the entity being edited by the form?

Upvotes: 2

Views: 1743

Answers (1)

datenimperator
datenimperator

Reputation: 308

You can simply add more attributes to the default ones:

<%= f.check_box :is_name_public, :class => 'someclass' %>

The same is true for the data attribute:

<%= f.check_box :is_name_public, :data => { :size => 'small', 'on-color'=>'success'} %>

Notice how the :data => { :size => 'small' … bit will be transformed into data-size="small" in the resulting HTML.

Upvotes: 5

Related Questions