I'm trying to store an array (:vehicles) in my database. This array consists of 3 possible vehicles that the user selects with checkboxes.
<% @vehicles.each_with_index do |vehicle,index| %>
<label>
<div >
<%= check_box_tag "vehicles[]", index ,class: "available" ,style: "display:none" %>
<%= vehicle %>
</div>
</label>
<%end %>
The ':vehicles' attribute type is 'text' and I used this code to make it an array:
serialize :vehicles, Array
If I send the params it gets these values in an array.
"vehicles"=>["0", "1", "2"],
"commit"=>"Sign up!",
"controller"=>"registrations",
"action"=>"create"
The problem is that this array isn't stored in the database when I try save it. In the database the code looks like this:
vehicles: []
Controller code:
def create
@student = Student.create(student_params)
if @student.save!
redirect_to @student
else
render :new
end
end
Student params:
def student_params
params.require(:student).permit(availabilities_attributes: [ :id, :day, :available])
end
Anyone have an idea how to get those values into this array?
Thanks!