Ruby on rails 具有多表继承的嵌套表单

Ruby on rails 具有多表继承的嵌套表单,ruby-on-rails,nested-forms,formtastic,multi-table-inheritance,Ruby On Rails,Nested Forms,Formtastic,Multi Table Inheritance,如何在rails中使用多表继承为对象构建嵌套表单?我正在尝试创建一个嵌套表单,以使用一个与另一组具有多表继承特性的模型有很多关系的模型来创建一个对象。我使用嵌套表单和gem的and来实现多表继承 我有以下型号: class Product < ActiveRecord::Base acts_as_superclass belongs_to :store end class Book < ActiveRecord::Base acts_as :product, :as

如何在rails中使用多表继承为对象构建嵌套表单?我正在尝试创建一个嵌套表单,以使用一个与另一组具有多表继承特性的模型有很多关系的模型来创建一个对象。我使用嵌套表单和gem的and来实现多表继承

我有以下型号:

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'
在阅读了github页面上的问题之后,我想让store和books之间的关系更加明确:

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
类产品true,:reject\u if=>:all\u blank
结束
类目:可生产
结束
类存储:产品
接受以下内容的\u嵌套\u属性\u:products、:allow\u destroy=>true、:reject\u if=>:all\u blank
接受以下内容的\u嵌套\u属性\u:books、:allow\u destroy=>true、:reject\u if=>:all\u blank
结束
现在,我得到一个无声的错误。我可以使用表单创建新的商店,cocoon允许我添加新书字段,但当我提交时,会创建商店,但不会创建子书籍。当我通过“/books/new”路径时,我可以毫无问题地创建一个跨越(products和books表)的新书记录

这个问题有解决办法吗?可以找到代码的其余部分。

也许您可以:

  • stores\u controller\new
    操作上手动建立图书关系

    @store.books.build

  • 手动存储您的关系
    stores#u controller#create
    action

    @store.books(对如何实现没有信心)


保留我们。

您可能需要考虑创建自己的窗体对象。这是RailsCast pro视频,但以下是AsciCast中的一些示例:

def new
  @signup_form = SignupForm.new(current_user)
end
此注册表单可以包括与其他对象的关系,就像在原始控制器代码中一样:

class SignupForm
    # Rails 4: include ActiveModel::Model
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations

  validates_presence_of :username
  validates_uniqueness_of :username
  validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/
  validates_length_of :password, minimum: 6

  def persisted?
    false
  end

    def subscribed
    subscribed_at
  end

  def subscribed=(checkbox)
    subscribed_at = Time.zone.now if checkbox == "1"
  end

  def generate_token
    begin
      self.token = SecureRandom.hex
    end while User.exists?(token: token)
  end
end
这是到RailsCast的链接。获得专业会员资格可能是值得的。我很幸运地通过www.codeschool.com获得了会员资格,在那里,当你完成课程时,你可以获得“奖品”:

铁路公司:

基于您在控制器中手动执行更多操作的想法,我现在尝试使用创建关联方法,但该方法似乎无法通过元编程自动连接。我最终找到了答案。一旦我清理了一些,我会发布解决方案,并确定使其工作所需的最小更改数。我给你奖金,因为你的答案最接近最终答案。谢谢!很抱歉无法回复您的评论。。。忙碌的一周。一定要发布解决方案并接受它,以便将问题标记为已回答。很高兴我能帮上忙。亲爱的sutee,你有没有机会分享一下解决这个问题的方法?我也有同样的问题,几天来一直在想如何解决它。目前,我们有一个充满丑陋黑客的表单,它只能创建对象,但我希望有一个完整的CRUD控制器。提前谢谢你@安东尼埃万格拉托夫,我已经向他承诺了我的解决方案。但是,我发现解决此问题会产生另一个问题:。我对这个问题没有任何答案,所以我暂时转移到其他问题上。如果你有任何想法,请告诉我!您是否检查了params散列以查看是否正确包含了子图书属性?
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
def new
  @signup_form = SignupForm.new(current_user)
end
class SignupForm
    # Rails 4: include ActiveModel::Model
  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations

  validates_presence_of :username
  validates_uniqueness_of :username
  validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/
  validates_length_of :password, minimum: 6

  def persisted?
    false
  end

    def subscribed
    subscribed_at
  end

  def subscribed=(checkbox)
    subscribed_at = Time.zone.now if checkbox == "1"
  end

  def generate_token
    begin
      self.token = SecureRandom.hex
    end while User.exists?(token: token)
  end
end