In my HTML, I am able to return an array from a select multiple box using
<select multiple id="purchases" name="purchases[]">
<option value="purchase1">Shoes</option>
<option value="purchase2">Dress Shirts</option>
</select>
My goal is to create a new database record for each of the options selected (I'm using Ruby on Rails and MySQL.) However, my controller isn't saving each value in its own record:
Controller
@purchase = Purchase.new(params[:purchases])
@purchase.purchaseinfo = params[:purchases]
Purchase Model
class Purchase < ActiveRecord::Base
belongs_to :customer
end
Customer Model
class Customer < ActiveRecord::Base
belongs_to :account
has_many :purchases
end
I know I should iterate through the controller, but I'm not sure how. Thanks in advance for your advice!
Edit 1
No matter what I do in the controller, the log tells me that the whole array, "purchases", is being sent, not the individual records. Here is the log and here is the current controller.
LOG
Processing SavedcustomerController#create (for IP at DATE TIME) [POST]
Parameters: {"purchases"=>["Item1", "Item2", "Item3"]}
Redirected to http://example.com/maptry
Completed in 21ms (DB: 2) | 302 Found [http://example.com/]
SavedcustomerController#Create
items_array = params[:purchases].split('"\"\"",')
items_array.each do |arrayitem|
@purchase = Purchase.new(params[:purchases])
@purchase.purchaseinfo = arrayitem
end