Trying to convert a form where previously only one option could be selected from multiple items using radio_button, to a form where multiple options can be selected using check_box.
Original code:
<% @inventory.each do |category, list| %>
<div class="col-xs-3">
<div class="form-group box">
<h5> <%="#{category.upcase}"%> </h5>
<% list.each do |thing| %>
<%= f.radio_button(:item, "#{thing}") %>
<%= f.label(:item, "#{thing}") %>
</br>
<% end %>
</div>
</div>
<% end %>
If all the nested each|dos seem confusing, basically what's happening is there are multiple categories of items being generated from an inventory hash. Each category looks distinct on the form, but a radio_button check on any item in any category counts as the one item you're selecting.
The class is "Request" and the column that this data posts to is "Item":
class Request < ActiveRecord::Base
validates :item, presence: true
I need to now make it so that a user can check any item in any category, and all of those items are POSTed as an array. I tried replacing the radio_button line with:
<%= f.check_box(:item, {:multiple => true}, "#{thing}") %>
It seems as though it's working, since I just tested and the Rails debugger shows the following:
request: !ruby/hash:ActionController::Parameters
item:
- '0'
- '0'
- Sleeping bag
- '0'
- Sleeping pad
However when I click the submit button, I get the error message that "Item cannot be blank."
Help?
EDIT: add controller code:
def new
@requestrecord = Request.new
inventory #This calls a private method that lists all the items by category and list
@pagetitle = "What would you like to borrow?"
end
def create
@requestrecord = Request.new(request_params)
inventory
@pagetitle = "What would you like to borrow?"
if @requestrecord.save
flash[:success] = "Thanks, we'll respond in a few hours. Below is the information you submitted in case you need to change anything."
@requestrecord.save_spreadsheet
RequestMailer.confirmation_email(@requestrecord).deliver
redirect_to edit_request_path(@requestrecord.edit_id)
else
render 'new'
end
end
private
def request_params
params.require(:request).permit(:email, :item, :detail, :rentdate, :edit_id)
end