Tack
Tack

Reputation: 121

How to insert new line breaks using form_for with collection_check_boxes

I use the following form_for at one point in my code to display a series of topics for users to select from.

 <%= form_for @user do |f| %> 
 <%= f.collection_check_boxes(:topic_ids, Topic.all.sample(50).each, :id, :topic_name) %> 
 <%= f.submit %>
 <% end %>

When it renders on the page, the boxes are all one-after-the-other. How can I separate them so that there is one check box per line?

Upvotes: 0

Views: 1355

Answers (3)

mechnicov
mechnicov

Reputation: 15357

It is very broad question. In short, using CSS.

For example using Bootstrap 4:

<%= form_for @user do |f| %>
<div class="form-check">
  <%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name, class: 'form-check-input' %>
</div>
<%= f.submit %>
<% end %>

Upvotes: 0

Anand
Anand

Reputation: 6531

You may of course render HTML inside the block:

   <%= form_for @user do |f| %> 
      <%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, : topic_name do |b| %>
      <div  class="my-checkbox-wrapper">
        <%= b.label(class: "foo") do %>
          <%= b.object.topic_name %>
          <%= b.check_box(class: "bar") ></br>
       <%end%>
       <%= f.submit %>
    <%end%>

You can have a look at this example

Upvotes: 2

widjajayd
widjajayd

Reputation: 6263

from reference here It is possibly to customize the way the elements will be shown by giving a block to the method as sample below from your code above

<%= form_for @user do |f| %> 
 <%= f.collection_check_boxes :topic_ids, Topic.all.sample(50).each, :id, :topic_name do |b| %> 
   <%= b.label(class: "check_box") do %>
     <%= b.check_box(class: "check_box") %>
     <%= b.object.topic_name %></br>
   <% end %>
 <% end %> 
 <%= f.submit %>
<% end %>

Upvotes: 3

Related Questions