【发布时间】:2015-10-07 09:22:15
【问题描述】:
我将 simple_form 与 cocoon (https://github.com/nathanvda/cocoon) 一起使用,一切正常。
我真正不喜欢的唯一一件事是我必须初始化空实例才能使其工作:
def new
@facility = Facility.friendly.find(params[:facility_slug])
@pet = @facility.pets.build(animal: Dog.new)
@pet.pet_pictures.build
@pet.animal.mixtures.build
end
最后两行是为了让 cocoon 和 link_to_add_association 工作,如果我删除它们,link_to_add_association 是完全空的。
有谁知道如何使它更符合习惯并避免显式调用 build 方法?如何改进此代码?
【问题讨论】:
-
link_to_add_association为空是什么意思?我用过 Cocoon 并且从来不需要构建项目来让它工作。带有关联的模型代码和视图代码中有什么? -
实际上很奇怪,我同意:基本上它不输出任何东西。您可以确认您是从头开始创建记录,而不是向已经存在的内容添加关联?
标签: ruby-on-rails ruby refactoring cocoon-gem