0

我被这个问题困住了。关于这个主题的 RoR 文档真的很糟糕。我不明白 collection_select 是如何工作的。

我有两个模型:技能和项目。一个项目有很多技能,一个技能有很多项目。

所以这是我的模式:

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

项目模型

class Project < ActiveRecord::Base

    has_and_belongs_to_many :skills

end

技能模型

class Skill < ActiveRecord::Base

    has_and_belongs_to_many :projects

end

和 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

选择似乎填充良好,但我的数据库中没有任何内容。

也许有人可以在我的代码中看到错误?

谢谢

4

0 回答 0