0

我正在尝试使用 Cocoon gem 为 Poi(兴趣点)对象创建具有嵌套属性的 Package 对象。当我创建一个新包时,我想添加现有的 Pois 来选择它们进行关联。但问题是当我创建一个新包时,我添加到这个包的 Pois 没有链接到它。因此,当我提交表单时,不会创建 package_pois 关系。

有我的代码:

class Package
  has_many :package_pois
  has_many :pois, :through => :package_pois, :class_name => 'Poi'

  belongs_to :user

  accepts_nested_attributes_for :pois
  accepts_nested_attributes_for :package_pois

end


class PackagePoi
  belongs_to :poi
  belongs_to :package

  accepts_nested_attributes_for :poi, :reject_if => :all_blank

end

包/_form.html.erb

<div id="package">
<%= f.simple_fields_for :package_pois do |poi| %>
   <%= render 'package_poi_fields', :f => poi %>
<% end %>
<%= link_to_add_association 'dd poi', f, :package_pois %>
</div>

包/ackage_poi_fields.html.erb

<div class="nested-fields package-poi-fields">
    <div id="pacakge_form_list">
    <%= f.association :poi, :collection => Poi.all(:order => 'title'), :prompt => 'Add existing poi' %>
  </div>
  <%= link_to_remove_association "Remove poi", f %>
</div>

和 package-new.js:

$("#package a.add_fields").
      data("association-insertion-position", 'before').
      data("association-insertion-node", 'this');

    $('#package').bind('insertion-callback',
         function() {
             $(".package-poi-fields a.add_fields").
                 data("association-insertion-position", 'before').
                 data("association-insertion-node", 'this');
             $('.package-poi-fields').bind('insertion-callback',
                  function() {
                    $(this).children("#pacakge_form_list").remove();
                    $(this).children("a.add_fields").hide();
                  });
         });

为什么它不创建 package_pois 关联?我能做什么?谢谢

4

1 回答 1

0

我修复了添加 :package_pois_attributes, :pois_attributes 作为 attr_accesible

于 2013-05-30T20:35:23.890 回答