连接模型附加属性的表单
Form for additional attributes of join model
一个用户通过 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) %>
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 %>
一个用户通过 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) %>
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 %>