jeudi 13 mai 2021

Getting {", value= "=>"on"} as a paramater when I want an Object's id

I have a program that is supposed to simulate a delivery POS. A Delivery Object belongs_to a Meal Object, which has_many items. The form renders a bunch of items with a checkbox next to each item using helper methods like this...

#inside the app/models/item.rb file
def self.meal_field_maker(foods)
  rStr = ""
  foods.each do | sel |
    rStr += "<p><input type= 'checkbox' name= 'meal[items][], value= '#{sel.id}'> #{sel.show} </p>"
  end
  return rStr
end

#inside the app/helpers/application_helper.rb file
def the_new_meal_form
  foodgroups = Item.get_foodgroups #Gets each food group
  rStr = ""
  foodgroups.each do | sel |
   rStr+= "\n<h3>#{sel}</h3>"  #Adds the name of each Food Group as a header before checkboxes
   groupedFoods = Item.list_items_of_group(sel) #Gets every item of a food group
   rStr += Item.meal_field_maker(groupedFoods)  #makes a checkbox input tag for each item in groupedFoods
  end
  return (rStr)
end

And this all renders properly in the form which looks like this...

<form method= "post" action= "new_user_delivery">
    <input type= "hidden" name= "delivery[user]" value= <%= @user.id %> 
    <%= user_delivery_address_field(session).html_safe %>
    <p>(Optional) Meal Name: <input type= "text" name="delivery[meal][name]"></p>
    <p>----------------------------------------------------</p>
    <%= the_new_meal_form.html_safe %>
    <p>----------------------------------------------------</p>
    <p>Proceed to Payment <input type= "submit" value= "Proceed">
</form>

And It looks like it all works perfectly, but on the next action the params look like this...

<ActionController::Parameters {"delivery"=>{"user"=>"11", "address"=>"98 Linden Ave", "meal"=>{"name"=>"FirstMeal"}}, "meal"=>{"items"=>[{", value= "=>"on"}, {", value= "=>"on"}, {", value= "=>"on"}, {", value= "=>"on"}, {", value= "=>"on"}]}, "controller"=>"deliveries", "action"=>"payment_options", "id"=>"11"} permitted: false>

The issue here is clearly that for every item I select, I just get {", value= "=>"on"}, which of course gives me no indication which Item Objects were chose, as you see this is ~supposed~ to return the Item's ID as the parameter value. How do I fix this?




Aucun commentaire:

Enregistrer un commentaire