Aaron C.
Aaron C.

Reputation: 51

Rails collection_check_boxes - wrap each checkbox with <li>

Brand new to Rails. How do I add a <li> wrapper to each checkbox / label element generated by the following code?

<%= f.collection_check_boxes :publish_to, [['YouTube'], ['Hulu'], ['Roku'], ['Owned Website'], ['Other']], :first, :first %>

The final outputted HTML would look like:

<li class="checkbox-wrap">
  <label></label>
  <input type="checkbox"/>
</li>

Thanks in advance!

Upvotes: 4

Views: 6418

Answers (2)

haris
haris

Reputation: 3875

It's possible. collection_check_boxes allows you to do it like this:

<ul>
<%= f.collection_check_boxes :publish_to, [['YouTube'], ['Hulu'], ['Roku'], ['Owned Website'], ['Other']], :first, :first do |b| %>
  <li>
    <%= b.label %>
    <%= b.check_box %>
  </li>
<% end %>
</ul>

Works on Rails 5 as well.

Reference: https://apidock.com/rails/v4.2.1/ActionView/Helpers/FormOptionsHelper/collection_check_boxes

Upvotes: 8

user4776684
user4776684

Reputation:

As per collection_check_boxes the following should do (though, I never tried it myself):

<%= f.collection_check_boxes :publish_to, [['YouTube'], ['Hulu'], ['Roku'], ['Owned Website'], ['Other']], :first, :first do |b| %>
   <%= content_tag :li, "#{b.label { b.check_box } }", class: "checkbox-wrap" %>
<% end %>

Or something very similar.

Upvotes: 1

Related Questions