jeudi 22 novembre 2018

How do I manage has_and_belongs_to_many relation for nested fields in Ruby on Rails

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" %>



Now I don't know how I manage this record in controller for create and update, And also I don't know how will I show this records.... If you understand my problem then pleasr provide me controller code for update and create of resume controller and also help me to show the resume data.

Aucun commentaire:

Enregistrer un commentaire