2

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?

6
  • Possible duplicate of In Rails, how to handle multiple checked checkboxes, just split on the , or? Commented Dec 13, 2017 at 16:30
  • Except the accepted answer in that answers in HTML not in Ruby. Which is why I asked. Commented Dec 13, 2017 at 16:35
  • 2
    Have you tried reading documentation? The first comment there seems to be what you want. Commented Dec 13, 2017 at 16:40
  • I've gone through the documentation. Tried some of the examples. That's why I posted here. I've tried the <%= f.checkbox :product, {}, "option1", "option2" %> and get the multiple arguments error. The first comment "works" in that it shows multiple options but doesn't actually display values. Commented Dec 13, 2017 at 16:48
  • @Jake: yes, checkboxes do not display anything. They're just checkboxes. If you need a caption next to each checkbox, that's your job. Pair each checkbox with a label_tag or something like that. Commented Dec 13, 2017 at 18:39

2 Answers 2

2

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 %>
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.