0
我嘗試使用Cocoon gem創建Poi(Point of interest)對象的嵌套屬性的Package對象。當我創建一個新的包時,我想添加現有的Pois爲關聯選擇它們。但問題是當我創建一個新的Package時,我添加到這個Package的Pois沒有鏈接到它。因此,我提交表單時不會創建package_pois關係。通過關聯simple_form和cocoon找不到has_many
有我的代碼:
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>
和包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是包裝和POI之間的關聯類,因此有兩個belongs_to的 – TomasMax