0
Cocoon gem을 사용하여 Poi (관심 대상) 객체에 대한 중첩 속성이있는 Package 객체를 만들려고합니다. 새 패키지를 만들 때 기존 Pois에 연관성을 선택하여 추가하려고합니다. 그러나 문제는 새로운 패키지를 만들 때입니다.이 패키지에 추가 한 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 같은 pois_attributes는 패키지 및 포이 사이의 연관 클래스는 따라서 두 belongs_to을 가지고있다 – TomasMax