连接模型的其他属性的表单

时间:2015-03-21 07:16:31

标签: ruby-on-rails ruby-on-rails-4 has-many-through

用户通过UserSkills拥有许多技能。 技能通过UserSkills拥有许多用户。

在我的新用户表单中,我可以添加技能复选框,但如果我在UserSkills模型中添加熟练程度(字符串),我该如何包含它?

我目前的代码:

<%= f.label :skills %>
<%= hidden_field_tag "user[skill_ids][]", nil %>
<% Skill.all.each do |skill| %>
<%= check_box_tag "user[skill_ids][]", skill.id, @user.skill_ids.include?(skill.id), id: dom_id(skill) %>
<%= link_to skill.skilltitle, skill_path(skill.id) %>

1 个答案:

答案 0 :(得分:0)

class User < ActiveRecord::Base
  has_many :user_skills
  accepts_nested_attributes_for :user_skills
end

class UserSkill < ActiveRecord::Base
  belongs_to :user
  belongs_to :skill

  accepts_nested_attributes_for :skill
  accepts_nested_attributes_for :user
end

...同时..以你的形式

<%= form_for @user do |f| %>
  <%= f.fields_for :user_skills do |f2| %>
    <%# add your user_skill attributes here %>
    <%= f2.check_box :proficiency %>
    <%= f2.fields_for :user do |f3| %>
      <%#  user attributes to go here %>
    <% end %>
<% end %>