Im trying to save multiple selected values form a multiple select field generated by the rails helper select.
<div class="form-group">
<%= f.label :available_type, "Available in category" %><br>
<%= f.select :available_type, options_for_select(Setting.subscription_type, @terminal.available_type), { }, { class: "form-control", :multiple => true, :size => 5 } %>
</div>
This renders like this (the selected value is from a previous attempt without the ":multiple => true" attribute that works perfectly):
<select class="form-control" id="terminal_available_type" multiple="multiple" name="terminal[available_type][]" size="5">
<option value="Postpaid">Postpaid</option>
<option value="MBB">MBB</option>
<option selected="selected" value="Prepaid">Prepaid</option>
</select>
Any help is appreciated. :)
Edit:
I have tried putting serialize :available_type
in my Terminal model, dident change anything. :-/
Edit 2: I noticed that the multiple selcted field does not mark options as selected when i mark them. if i add Selected attributes manualy i get these parametres:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"RrwWlKk8XlGeC+dTu/w6oSM68e9LcbUFJWTI+eRS9mI=", "terminal"=>{"inndate"=>"2015-01-13", "outdate"=>"", "brand_id"=>"2", "name"=>"iPhone 5c", "available_type"=>["", "MBB", "Prepaid"], "product_number"=>"3r2342", "ean_code"=>"", "navision_nb"=>"324234", "cost_price_map"=>"3200.0", "manual_price"=>"", "sales_info"=>"Just sell!"}, "commit"=>"Submit", "action"=>"update", "controller"=>"terminals", "id"=>"2"}
The available_type field has the value "available_type"=>["", "MBB", "Prepaid"]
Im using rails 4.0.2 and here are my strong parametres:
# Never trust parameters from the scary internet, only allow the white list through.
def terminal_params
params.require(:terminal).permit(:inndate, :outdate, :brand_id, :name, :product_number, :navision_nb, :cost_price_map, :manual_price, :sales_info, :available_type)
end
Finaly found the answer!
This problem was a combo of PG and Rails 4.
First i needed to convert the column from a string to a text column marked as array like so:
class ChangeAvailableTypeOnTerminals < ActiveRecord::Migration
def up
change_column :terminals, :available_type, :text, array: true, default: []
end
def down
change_column :terminals, :available_type, :string
end
end
Then i needed to treat the strong parametre as an array in the terminals controller like so:
# Never trust parameters from the scary internet, only allow the white list through.
def terminal_params
params.require(:terminal).permit(:inndate, :outdate, {:available_type => []}, :brand_id, :name, :product_number, :navision_nb, :cost_price_map, :manual_price, :sales_info)
end
To be more spesific:
Change from :available_type
to {:available_type => []}
This solved the issue for me. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With