레일에 다중 테이블 상속을 사용하여 개체의 중첩 된 양식을 작성하려면 어떻게해야합니까? 다중 테이블 상속 기능을 가진 다른 모델 집합과 has_many 관계가있는 모델을 사용하여 개체를 만드는 중첩 된 폼을 만들려고합니다. 중첩 된 양식에는 formtastic과 cocoon을 사용하고 복수 테이블 상속을 구현하려면 act_as_relation 보석을 사용하고 있습니다.다중 테이블 상속을 사용하는 중첩 된 양식
나는 다음과 같은 모델이이 예를 들어
class Product < ActiveRecord::Base
acts_as_superclass
belongs_to :store
end
class Book < ActiveRecord::Base
acts_as :product, :as => :producible
end
class Pen < ActiveRecord::Base
acts_as :product, :as => :producible acts_as :product, :as => :producible
end
class Store < ActiveRecord::Base
has_many :products
accepts_nested_attributes_for :products, :allow_destroy => true, :reject_if => :all_blank
end'
을,이 책은 다른 제품에 비해 한 유일한 고유 한 속성 것을 저자 필드입니다. 실제로는 책에 대한 고유 한 속성이 많아서 더 일반적인 단일 테이블 상속에 비해 다중 테이블 상속을 선택했습니다.
제품이있는 새 저장소를 만들 수있는 중첩 된 양식을 만들려고합니다. 여기 내 양식이다 :
<%= semantic_form_for @store do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<h3>Books/h3>
<div id='books'>
<%= f.semantic_fields_for :books do |book| %>
<%= render 'book_fields', :f => book %>
<% end %>
<div class='links'>
<%= link_to_add_association 'add book', f, :books %>
</div>
<% end %>
<%= f.actions :submit %>
<% end %>
그리고 book_fields 부분 : act_as_relation의 GitHub의 페이지에서 문제를 읽고 바탕으로
undefined method `new_record?' for nil:NilClass
, 나에 대한 생각 :
<div class='nested-fields'>
<%= f.inputs do %>
<%= f.input :author %>
<%= link_to_remove_association "remove book", f %>
<% end %>
</div>
나는이 오류 상점과 책의 관계를보다 명확하게 만듭니다.
class Product < ActiveRecord::Base
acts_as_superclass
belongs_to :store
has_one :book
accepts_nested_attributes_for :book, :allow_destroy => true, :reject_if => :all_blank
end
class Book < ActiveRecord::Base
belongs_to :store
acts_as :product, :as => :producible
end
class Store < ActiveRecord::Base
has_many :products
has_many :books, :through => :products
accepts_nested_attributes_for :products, :allow_destroy => true, :reject_if => :all_blank
accepts_nested_attributes_for :books, :allow_destroy => true, :reject_if => :all_blank
end
이제 침묵 오류가 발생합니다. 양식을 사용하여 새 상점을 만들 수 있으며 고치기를 사용하면 새 서평 필드를 추가 할 수 있지만 제출하면 저장소가 생성되지만 하위 책은 생성되지 않습니다. `/ books/new '경로를 살펴볼 때 문제가없는 새로운 책 레코드 (제품과 책 테이블)를 만들 수 있습니다.
이 문제를 해결할 수있는 방법이 있습니까? 나머지 코드는 here입니다.
당신이 볼 수있는 PARAMS 해시를 확인해 봤어을 아동 도서 속성 경우 제대로 포함 되었습니까? –