Search code examples
ruby-on-railsrubymongoidform-forfields-for

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


Solution

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