Ruby on rails 如何使用cocoon同时创建父元素和嵌套元素
我有一个Ruby on rails 如何使用cocoon同时创建父元素和嵌套元素,ruby-on-rails,ruby-on-rails-5,cocoon-gem,Ruby On Rails,Ruby On Rails 5,Cocoon Gem,我有一个版本模型,它接受内容模型的嵌套属性。我是否可以在创建新版本的同时创建嵌套内容 创建版本时,会出现以下错误: 目录版本不能为空 据我所知,这是因为该版本尚未创建,因此,没有edition\u id进入目录表 我曾尝试在版本和内容模型中设置inverse\u of选项,但没有成功。我仍然得到这个错误 以下是我的版本和内容模型: class Content < ApplicationRecord validates_presence_of :heading,
版本
模型,它接受内容
模型的嵌套属性。我是否可以在创建新版本的同时创建嵌套内容
创建版本时,会出现以下错误:
目录版本不能为空
据我所知,这是因为该版本尚未创建,因此,没有edition\u id
进入目录表
我曾尝试在版本和内容模型中设置inverse\u of
选项,但没有成功。我仍然得到这个错误
以下是我的版本和内容模型:
class Content < ApplicationRecord
validates_presence_of :heading,
:link,
:edition_id,
:section_id
belongs_to :edition, inverse_of: :contents
belongs_to :section
belongs_to :source, inverse_of: :contents
end
class Edition < ApplicationRecord
validates_presence_of :date, :product_id
belongs_to :product
has_many :contents, dependent: :destroy, inverse_of: :edition
has_many :sections, -> { distinct }, through: :contents
accepts_nested_attributes_for :contents,
allow_destroy: true,
reject_if: lambda { |attrs| attrs['link'].blank? }
end
您可以在关联中添加可选:true这将解决您的错误,如下所示
belongs_to :edition, optional: true
就我所知,您必须首先为
嵌套属性构建对象,即
def new
@edition = Edition.new
contents = @edition.contents.build
end
并在中创建
操作
def create
@edition = Edition.new(edition_params)
respond_to do |format|
if @edition.save
format.html { redirect_to @edition, notice: 'Edition was successfully created.' }
format.json { render :show, status: :created, location: @edition }
else
format.html { render :new }
format.json { render json: @edition.errors, status: :unprocessable_entity }
end
end
end
def edition_params
params.require(:edition).permit(:date,
:clicks,
:product_id,
contents_attributes: [:id,
:heading,
:body,
:link,
:top_story,
:section_id,
:_destroy
]
)
end
删除版本id的验证
class Content < ApplicationRecord
validates_presence_of :heading,
:link,
:section_id
belongs_to :edition, inverse_of: :contents
belongs_to :section
belongs_to :source, inverse_of: :contents
end
类内容
@JagdeepSingh我已经用服务器日志编辑了这个问题,添加了创建操作和用于创建操作的参数,@JagdeepSingh:)请尝试下面给出的解决方案,并让我知道进一步的指导,如果答案对您有帮助,也可以随意接受/投票。可选:true
这将很容易让您在没有父对象的情况下创建子对象,这是不对的。是的,它将很容易让您在没有父对象的情况下创建子对象,但这取决于要求,如果子对象并不总是需要父对象,您可以选择可选:true谢谢!但是,在我的情况下,家长是必需的。本质上,没有版本的内容不应该存在。好的,那么如果需要父级@edition,就不要使用optional:true。contents.build
将返回内容
(单数)。@JagdeepSingh同意!但据我所知,cocoon
gem可以处理我们想要创建的尽可能多的嵌套对象,但首先它需要提供一个构建嵌套对象。不,不起作用:(.我不确定问题出在内容上。我想只是版本还没有创建,所以没有id
分配给嵌套内容。有没有办法创建版本然后创建内容?也就是说,按顺序创建?@UmarGhouse checkout this@UmarGhouse你能检查更新的ans吗wer,正如我所看到的,您已经添加了content\u id
witn validation presence true,仅供参考,在rails 5中,属于:content
意味着它也是必需的。请将其从验证中删除,然后重试,希望现在可以正常工作。
def create
@edition = Edition.new(edition_params)
respond_to do |format|
if @edition.save
format.html { redirect_to @edition, notice: 'Edition was successfully created.' }
format.json { render :show, status: :created, location: @edition }
else
format.html { render :new }
format.json { render json: @edition.errors, status: :unprocessable_entity }
end
end
end
def edition_params
params.require(:edition).permit(:date,
:clicks,
:product_id,
contents_attributes: [:id,
:heading,
:body,
:link,
:top_story,
:section_id,
:_destroy
]
)
end
class Content < ApplicationRecord
validates_presence_of :heading,
:link,
:section_id
belongs_to :edition, inverse_of: :contents
belongs_to :section
belongs_to :source, inverse_of: :contents
end