Jake
Jake

Reputation: 1370

How to add in multiple options for checkbox in Ruby on Rails?

This is likely a super easy answer for a Ruby on Rails expert. I have a form and need to add in a checkbox that has multiple items. I've been messing with the following code for a lot longer than I'd like to admit:

<%= form_for :lead, url: something, html: {id: 'product-form'} do |f|%>
<div class="row">
      <div class="col-md-12">
        <div class="form-group">
          <%= f.label :product%>
          <%= f.check_box :product, {multiple:true}, "option", "option2",   :class => 'form-control'%>
        </div>
      </div>
    </div>
<% end %>

With this code I get the error "wrong number of arguments (5 for 1..4)".

Basically I just want someone to be able to pick multiple options. I've also tried the following:

<div class="row">
      <div class="col-md-12">
        <div class="form-group">
          <%= f.label :product%>
          <%= f.check_box :option1, "Option1" :class => 'form-control'%>
          <%= f.check_box :option2, "Option2", :class => 'form-control'%>
        </div>
      </div>
    </div>

And I get the delightful "undefined method `merge' for "Option1":String". What am I missing to put the values in associated with the label?

Upvotes: 2

Views: 1992

Answers (2)

Reco Daley
Reco Daley

Reputation: 129

I use Rails 5.1.6 and this is how I achieved adding multiple options for checkbox. I also placed it in a dropdown.

In the migration file:

t.string :skills

In my controller "tasks_controller.rb":

def task_params
  params.require(:task).permit(:work_type, :title, :post, {skills: []})
end

In my model "task.rb" I did:

validates :skills, presence: true
serialize :skills, JSON

I created a module mobilephones_data.rb in directory "app/model/concerns/" which holds all the options of the checkbox as an array that can be edited easily.

In app/model/concerns/mobilephones_data.rb I wrote:

 module Mobilenphones_Data
 Activities = [
    'Amazon Kindle',  'Blackberry',  'iPad',  'iPhone',  'Mobile Phone',  'Nokia',  
    'Palm',  'Samsung'
]
end

This will put all data from module's array into the checkbox drop-down as checkbox options. In My form I did:

    <div class="card">
        <a class="card-link card-header" data-toggle="collapse" href="#collapseOne">
          Mobile Phones
        </a>

      <div id="collapseOne" class="collapse" data-parent="#accordion">
        <div class="card-body">
          <% Mobilenphones_Data::Activities.each do |activity| %>
            <div id="skill_list" class="col-lg-3 col-md-4 col-sm-12 col-12">
              <%= f.check_box :skills, { multiple: true }, activity, false %> 
              <%= activity %>
            </div>
          <% end %>
        </div>
      </div>
    </div> <!--bottom-->
</div>

In my view "show.html.erb":

<% @name_of_model.skills.each do |skill| %>
    <%= skill %>
<% end %>

Upvotes: 2

Jake
Jake

Reputation: 1370

For posterity sake:

<div class="row">
    <div class="col-md-12">
      <div class="form-group">
      <%= f.label "Select a Product" %><br />
      <%= f.check_box(:option) %>
      <%= f.label(:mug, "Option") %><br />
      <%= f.check_box(:option2) %>
      <%= f.label(:mousepad, "Option2") %>
      </div>
    </div>
  </div>

Upvotes: 0

Related Questions