LuigiAlver LuigiAlver - 6 months ago 84
CSS Question

Materialize css checkbox not working in rails app

I'm trying to add checkboxes to a form for users to select the workshops that they want but I cannot make the checkboxes appear. I'm using Materialize Css gem with rails.

My form is the next:

<div class="row">
<% workshops = Workshop.all.order("date") %>
<% workshops.each do |workshop| %>
<div class=" col m4 s6">
<%= f.label "#{} \n\r #{}", for:"workshop_id", id:"workshop_id" %>
<%= f.check_box :workshop_id, id:"workshop_id" %>
<% end %>
<div class="actions col m6 s12">
<%= f.submit "Save", class:"btn green"%>

I have tried with and without the id and for attributes inside the input and the label, but nothing seems to work.
Am I missing something?


The css that shows from Materialize css is the following:

[type="checkbox"]:not(:checked), [type="checkbox"]:checked {
position: absolute;
left: -9999px;
visibility: hidden;

Answer Source

The solution, as simple as it is... Was to change the order of the <input> and the <label>

So instead of:

<%= f.label "#{} \n\r #{}", for:"workshop_id", id:"workshop_id" %>`
<%= f.check_box :workshop_id, id:"workshop_id" %>`

The righ way is:

<%= f.check_box :workshop_id, id:"workshop_id" %>
<%= f.label "#{} \n\r #{}", for:"workshop_id", id:"workshop_id" %>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download