Ruby on Rails中的多个表关联表单

时间:2012-07-24 03:34:39

标签: ruby-on-rails-3

目前我在rails项目上遇到了我的ruby。我正在使用rails 3.2

我设法为人体形式的技能创建复选框,但我无法弄清楚如何创建一个表单输入,它可以更改human_skills表中的“for_work”布尔值。

所以我的目标是当用户创建新人或编辑人时他们可以选择技能并确定技能是否适合工作(布尔值)。

提前非常感谢!

Here is my Human form code

    = semantic_form_for @human do |f|
    = f.input :name 
    = f.input :skills , :as => :check_boxes,

数据库设计

技能 - id,name

human_skills - id,skills_id,human_id,for_work

人类 - id,name

这是我的型号代码:

class Human < ActiveRecord::Base
  has_many :human_skills;
  has_many :skills, :through => :human_skills

class HumanSkill< ActiveRecord::Base
    belongs_to :human_skills;
    belongs_to :human;

class Skill < ActiveRecord::Base
  has_many :human_skills;
  has_many :human, :through => :human_skills

1 个答案:

答案 0 :(得分:1)

好的,所以你需要稍微调整模型以允许人类创建技能,然后使用fields_for让form_for帮助者知道它在另一个模型上。

class Human < ActiveRecord::Base
  has_many :human_skills;
  has_many :skills, :through => :human_skills

  accepts_nested_attributes_for :skills

class HumanSkill< ActiveRecord::Base
    belongs_to :human_skills;
    belongs_to :human;

class Skill < ActiveRecord::Base
  has_many :human_skills;
  has_many :human, :through => :human_skills

和表格

 = semantic_form_for @human do |f|
    = f.input :name 
    = fields_for @human.skills do |h_skills|
      = h_skills.checkbox :skills 

要检出以回答您的问题的资源:接受嵌套属性= Accepts_nested_attributes_for字段对于文档:Fields_for 嵌套表格视频Railscast