Mongoid embeds_many 与 Rails fields_for
我有一个像这样的模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的形式看起来不错 - 但我注意到你的控制器的操作中有一个拼写错误。不确定这是否只是您的问题或实际应用程序中的拼写错误,但
应该是
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
should be
这个问题有我的答案。
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.