如何管理RubyonRails中嵌套字段的多对多关系


class Resume < ActiveRecord::Base
has_many :user_skills, :dependent => :destroy
accepts_nested_attributes_for :user_skills, :allow_destroy => true, :reject_if => :all_blank 
end
class UserSkill < ActiveRecord::Base
belongs_to :resume
has_and_belongs_to_many :technologies
end
class Technology < ActiveRecord::Base
has_and_belongs_to_many :user_skills
end

<%= nested_form_for([:student, @resume], validate: true, :html => { :multipart => true, class: "full-width" }) do |f| %>

------------------------------
Resume fields
------------------------------      
<h5>User Skills</h5>
<%= f.fields_for :user_skills do |us| %>
<%= us.label :academic_years, "Academic Years" %>
<%= us.text_field :academic_years %>
<%= us.label :professional_years, "Professional Years" %>
<%= us.text_field :professional_years %>
<%= us.fields_for :technologies do |tech| %>
<%= tech.collection_select :name, Technology.all, :id, :name, { prompt: "Select Technology"}, { :multiple => true, :size => 10} %> 
<% end %>
<%= us.link_to_remove "Remove", class: "btn btn-small red right" %>


现在我不知道我如何在控制器中管理这个记录以进行创建和更新,也不知道我将如何显示这些记录。。。。如果你理解我的问题,那么请为我提供控制器代码,用于更新和创建简历控制器,并帮助我显示简历数据。

我认为您使用了Ryan Bates的旧nested_form宝石。您应该使用最新的,例如simple_form或来自ruby-tooolbox.com 的其他

最新更新