I'm stuck with this issue. The RoR doc about this topic is really awful. I don't understand how collection_select works.
I have two models : Skills and Projects. A Project have many skills and a skill have many projects.
So here are my schemas :
create_table "projects", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "projects_skills", id: false, force: true do |t|
t.integer "project_id"
t.integer "skill_id"
end
create_table "skills", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "mastering"
end
The Project Model
class Project < ActiveRecord::Base
has_and_belongs_to_many :skills
end
The Skill Model
class Skill < ActiveRecord::Base
has_and_belongs_to_many :projects
end
And the collection_select
= form_for(instance_variable_get('@' + controller.controller_name.singularize)) do |f|
.field
= f.label :skills
= collection_select(:skills, :id, Skill.all, :id, :name, {:multiple=>true})
.actions
= f.submit
The select appears well populated, but nothing is persisted in my database.
Maybe someone can see a mistake in my code ?
Thanks