【问题标题】:How to manage many to many relation for nested fields in Ruby on Rails如何在 Ruby on Rails 中管理嵌套字段的多对多关系
【发布时间】:2019-04-25 11:20:41
【问题描述】:
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" %>


现在我不知道如何在控制器中管理此记录以进行创建和更新,而且我也不知道如何显示此记录...。如果您了解我的问题,那么请为我提供更新和创建恢复控制器的控制器代码,并帮助我显示恢复数据。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2


    【解决方案1】:

    我认为您使用的是 Ryan Bates 的旧 nested_form gem。您应该使用最新的,例如 simple_form 或来自 ruby-toolbox.com 的其他人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多