Ruby on rails 通过关联(嵌套)创建多个新操作的示例
我在以下位置找到此代码: 它将用户和出版物链接在一起,并在出版物模型中使用正确的user_id字段填充(attr_accessible中未列出),因此效果非常好 现在我的挑战是在出版物中增加问题,这是我的不足之处。我有一个可以添加问题的菜单,但我不希望表单中的publication_id字段,也不希望模型中的attr_中可以访问它。我想通过用户使用所选出版物创建一个问题Ruby on rails 通过关联(嵌套)创建多个新操作的示例,ruby-on-rails,has-many,Ruby On Rails,Has Many,我在以下位置找到此代码: 它将用户和出版物链接在一起,并在出版物模型中使用正确的user_id字段填充(attr_accessible中未列出),因此效果非常好 现在我的挑战是在出版物中增加问题,这是我的不足之处。我有一个可以添加问题的菜单,但我不希望表单中的publication_id字段,也不希望模型中的attr_中可以访问它。我想通过用户使用所选出版物创建一个问题 如果我不能很好地解释,我很抱歉,这对我来说还是很新鲜的,可能也是我在寻找正确术语时遇到困难的原因。希望这能帮助您: 型号: c
如果我不能很好地解释,我很抱歉,这对我来说还是很新鲜的,可能也是我在寻找正确术语时遇到困难的原因。希望这能帮助您: 型号:
class Document < ActiveRecord::Base
has_many :sections
has_many :paragraphs, :through => :sections
accepts_nested_attributes_for :sections, :allow_destroy => true
accepts_nested_attributes_for :paragraphs, :allow_destroy => true
end
class Section < ActiveRecord::Base
belongs_to :document
has_many :paragraphs
end
class Paragraph < ActiveRecord::Base
belongs_to :section
end
谢谢希望这对您有所帮助: 型号:
class Document < ActiveRecord::Base
has_many :sections
has_many :paragraphs, :through => :sections
accepts_nested_attributes_for :sections, :allow_destroy => true
accepts_nested_attributes_for :paragraphs, :allow_destroy => true
end
class Section < ActiveRecord::Base
belongs_to :document
has_many :paragraphs
end
class Paragraph < ActiveRecord::Base
belongs_to :section
end
谢谢我非常感谢您的帮助,也感谢您的快速回复。也许我需要澄清一下。我有3个模型(用户、出版物、发行版),它们在各自的视图和控制器中是分开的。我们的目标是拥有一个控制面板,登录用户可以单击以下链接:a)添加/编辑/删除与个人用户相关的出版物b)添加/编辑/删除与个人出版物相关的问题,因此我还有3个单独的表单(用户,出版物问题)。我非常感谢您的帮助,并感谢您的快速回复。也许我需要澄清一下。我有3个模型(用户、出版物、发行版),它们在各自的视图和控制器中是分开的。目标是拥有一个控制面板,登录用户可以在其中单击以下链接:a)添加/编辑/删除与个人用户相关的出版物b)添加/编辑/删除与个人出版物相关的问题,因此我还有3个单独的表单(用户,出版物问题)。
class Document < ActiveRecord::Base
has_many :sections
has_many :paragraphs, :through => :sections
accepts_nested_attributes_for :sections, :allow_destroy => true
accepts_nested_attributes_for :paragraphs, :allow_destroy => true
end
class Section < ActiveRecord::Base
belongs_to :document
has_many :paragraphs
end
class Paragraph < ActiveRecord::Base
belongs_to :section
end
class DocumentsController < ApplicationController
def new
@document = Document.new
@document.sections.build
@document.paragraphs.build
end
end
form_for @document do |f|
----document fields----------
f.fields_for :sections do |s|
----sections fields----------
end
f.fields_for :paragraphs do |s|
----paragraphs fields----------
end
end