【问题标题】:add has_many through association with simple_form and cocoon not found通过与 simple_form 和 cocoon 的关联添加 has_many 未找到
【发布时间】:2013-05-29 22:41:09
【问题描述】:

我正在尝试使用 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 关联?我能做什么? 谢谢

【问题讨论】:

  • 没有PackagePoi是Package和Poi的关联类,所以它有两个belongs_to

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多