gpt4 book ai didi

ruby-on-rails - 带有 Rails fields_for 的 Mongoid embeds_many

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

我有这样一个模型:

class Search
include Mongoid::Document

embeds_many :terms

accepts_nested_attributes_for :terms
end

class Terms
include Mongoid::Document

embedded_in :search, inverse_of: :terms

field :some, type: String
field :search, type: String
field :terms, type: String
end

我有一些火腿,看起来像:

= form_for @search do |f|
- f.fields_for(:terms) do |term_form|
= term_form.label :some
= term_form.text_field :some
= term_form.label :search
= term_form.text_field :search
= f.submit 'Save'

我的 Search#new 方法如下所示:

@search = Search.new
@search.terms.build

如果页面上出现任何内容,我会很高兴,但事实并非如此。

如何使用 form_for 和 fields_for 为 Mongoid embeds_many 嵌入式文档制作表单?

郑重声明,我也试过像这样的 haml:

= form_for @search do |f|
- @search.terms.each do |term|
- f.fields_for(term) do |term_form|
....

和其他一些变体,都无济于事。

最佳答案

您的表单看起来不错 - 但我注意到您的 Controller 操作中有错字。不确定这只是您问题中的错字,还是您实际应用中的错字,但是

@search.parties.build

应该是

@search.terms.build

关于ruby-on-rails - 带有 Rails fields_for 的 Mongoid embeds_many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6917963/

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