通过与simple_form和找不到cocoon的关联添加has_many

时间:2013-05-29 22:41:10

标签: ruby-on-rails-3 gem nested-forms cocoon-gem

我正在尝试使用Cocoon gem为Poi(Point of interest)对象创建具有嵌套属性的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关联?我能做什么? 感谢

1 个答案:

答案 0 :(得分:0)

我修复了添加:package_pois_attributes,:pois_attributes as attr_accesible