Mongoid embeds_many with Rails fields_for
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.term开发者_StackOverflow中文版s.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.
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
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.
精彩评论