sysconfig
sysconfig

Reputation: 431

fields_for with mongoid embeds_many nested documents

attempting to use this railscast as a guide: http://railscasts.com/episodes/197-nested-model-form-part-2?view=asciicast

and running into this error:

`@search[queries_attributes][new_queries][queries' is not allowed as an instance variable name

models:

#search.rb
class Search
  include Mongoid::Document
  include Mongoid::Timestamps


  belongs_to :user
  field :name,              :type => String

  embeds_many :queries
  accepts_nested_attributes_for :queries, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true

#query.rb
class Query
  include Mongoid::Document

  field :columns,           :type => String
  field :types,             :type => String
  field :keywords,          :type => String

  embedded_in :search, :inverse_of => :queries 

end

searches controller :

def new
  @search = Search.new
  @search.queries.build
  #3.times { @search.queries.build }
end

_form.html.haml partial:

= form_for(@search) do |f|
  = f.label 'Name this search'
  = f.text_field :name, :class => 'text_field'
  = render :partial => 'query', :collection => @search.queries, :locals => { :f => f }
  = link_to_add_fields "Add Query", f, :queries 

  .actions
    = f.submit

_query.html.haml partial:

.fields
  = f.fields_for "queries[]", query do |q|

    = q.label 'Search Datatype'
    = q.select :types, Query::TYPES

    = q.label 'In Column'
    = q.select :columns, @search.record_columns

    = q.label 'For Keywords:'
    = q.text_field :keywords, :class => 'text_field'

    = q.hidden_field :_destroy 
    = link_to_function "remove", "remove_fields(this)" 

searches helper:

module SearchesHelper

    def link_to_add_fields(name, f, association)
      new_object = f.object.class.reflect_on_association(association).klass.new
      fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
        render(association.to_s.singularize , :f => builder)
      end
      link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
    end

end

javascript:

function remove_fields(link) {
        $(link).prev("input[type=hidden]").val("1");
        $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
        var new_id = new Date().getTime();
        var regexp = new RegExp("new_" + association, "g");
        $(link).parent().before(content.replace(regexp, new_id));
}

when the line:

= link_to_add_fields "Add Query", f, :queries

is commented out, it works as expected, but I need to be able to add additional queries via this helper. for testing multi queries I am triggering the creation in the controller 3.times

also in the error message the last "]" is stripped off.. not sure what I am missing

sorry for all the tags, but not sure where the issue lies

Upvotes: 1

Views: 1415

Answers (2)

sysconfig
sysconfig

Reputation: 431

looks like this was the fix:

= f.fields_for :queries, query do |q|

Upvotes: 2

Tyler Brock
Tyler Brock

Reputation: 30136

Two thoughts:

I would name the Query class something else, it probably conflicts with some stuff inside mongoid as per the error message you specified:

@search[queries_attributes][new_queries][queries' is not allowed as an instance variable name]

Also googling your problem I came across this:

http://www.jtanium.com/2009/11/03/rails-fields_for-is-not-allowed-as-an-instance-variable-name/

Something must be nil where it shouldn't be.

Upvotes: 1

Related Questions