Mongoid embeds_many 与 Rails fields_for

发布于 2024-11-27 11:06:14 字数 979 浏览 2 评论 0原文

我有一个像这样的模型:

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

我有一些看起来像这样的 haml:

= 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|
....

以及其他一些变体,但均无济于事。

I have a model like this:

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

and I have some haml that looks like:

= 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'

my Search#new method looks like:

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

and I would love it if anything showed up on the page, but it doesn't.

How do I make a form using form_for and fields_for the a Mongoid embeds_many embedded document?

For the record, I have also tried haml that looks like:

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

and a few other variations, all to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-12-04 11:06:14

你的形式看起来不错 - 但我注意到你的控制器的操作中有一个拼写错误。不确定这是否只是您的问题或实际应用程序中的拼写错误,但

@search.parties.build

应该是

@search.terms.build

Your form seems fine - but i noticed there's a typo in your controller's action. Not sure if it's just a typo in your question, or in your actual app, but

@search.parties.build

should be

@search.terms.build
沫离伤花 2024-12-04 11:06:14

这个问题有我的答案。

Rails 3 希望

- f.fields_for(:terms) do |term_form| 为:

= f.fields_for(:terms) do |term_form|

所以事实证明它有与 mongoid 关系完全无关。我真是愚蠢的假设。

this question had my answer.

rails 3 wants

- f.fields_for(:terms) do |term_form| to be:

= f.fields_for(:terms) do |term_form|

so it turns out it has absolutely nothing to do with mongoid relations at all. Stupid me for assuming.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文