gpt4 book ai didi

ruby-on-rails - fields_for 表单生成器对象为零

转载 作者:数据小太阳 更新时间:2023-10-29 06:23:58 25 4
gpt4 key购买 nike

有什么方法可以访问嵌套的 form_bulder.object?

## controller
@project = Project.new
@project.tasks.build

form_for(@project) do |f|
f.object.nil? ## returns false

fields_for :tasks do |builder|
builder.object.nil? ## returns true
end
end

最佳答案

您必须在项目模型中有 accepts_nested_attributes_for 才能传递对象。

class Project < ActiveRecord::Base
has_many :tasks
accepts_nested_attributes_for :tasks ## this is required
end

关于ruby-on-rails - fields_for 表单生成器对象为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2409638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com