ruby on rails - User selects multiple items using check_box form helper; POSTs data as array -
trying convert form 1 option selected multiple items using radio_button, form multiple options can selected using check_box.
original code:
<% @inventory.each |category, list| %> <div class="col-xs-3"> <div class="form-group box"> <h5> <%="#{category.upcase}"%> </h5> <% list.each |thing| %> <%= f.radio_button(:item, "#{thing}") %> <%= f.label(:item, "#{thing}") %> </br> <% end %> </div> </div> <% end %>
if nested each|dos seem confusing, what's happening there multiple categories of items being generated inventory hash. each category looks distinct on form, radio_button check on item in category counts 1 item you're selecting.
the class "request" , column data posts "item":
class request < activerecord::base validates :item, presence: true
i need make user can check item in category, , of items posted array. tried replacing radio_button line with:
<%= f.check_box(:item, {:multiple => true}, "#{thing}") %>
it seems though it's working, since tested , rails debugger shows following:
request: !ruby/hash:actioncontroller::parameters item: - '0' - '0' - sleeping bag - '0' - sleeping pad
however when click submit button, error message "item cannot blank."
help?
edit: add controller code:
def new @requestrecord = request.new inventory #this calls private method lists items category , list @pagetitle = "what borrow?" end def create @requestrecord = request.new(request_params) inventory @pagetitle = "what borrow?" if @requestrecord.save flash[:success] = "thanks, we'll respond in few hours. below information submitted in case need 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
you making typical mistake when handling array type of form parameters , strong parameters.
you need tell controller it's ok accept array of "items".
so, change definition of request_params
liike this:
def request_params params.require(:request).permit(:email, {:item => []}, :detail, :rentdate, :edit_id) end
and should on error.
Comments
Post a Comment