nathanvda/cocoon

ActionView::Template::Error (uninitialized constant) when using Mongoid Embeds Many Association

boyfunky opened this issue · 1 comments

So i have the following model

class Category
      include Mongoid::Document
      field :slug, type: String
      embeds_many :translations
      embeds_many :segments
      accepts_nested_attributes_for :translations, :reject_if => :all_blank, :allow_destroy => true
    end

and in my HTML using simple_form, i have the following:

<%= simple_form_for @category do |f| %>
      <%=f.input :slug, label: 'Slug', required: true %>
      <%= f.submit class:'btn btn-primary text-uppercase' %>
      <div class="col-sm-12">
        <div class="card">
          <div class="card-header">
            <div class="row">
              <div class="col-sm-12">
                <i class="fa fa-align-justify"></i>Translation
                <%=link_to_add_association f, :translations, partial: 'shared/translation', 'data-association-insertion-method' => 'append' do %>
                  <span class="right">
                    <i class="icon-add-new"></i>
                    Add Translation
                  </span>
                <% end %>
              </div>
            </div>
          </div>
          <div class="card-body">
            <div id="translations" class="row">
              <%= f.simple_fields_for :translations do |translation| %>
                <%= render 'shared/translation', f: translation %>
              <% end %>
              <div class="clear"></div>
            </div>
          </div>
        </div>
      </div>
    <%end %>

In my controller. i have this

def new
   @category = Category.new
 end

But whenever i try to load the new.html.erb, this block of code

<%=link_to_add_association f, :translations, partial: 'shared/translation', 'data-association-insertion-method' => 'append' do %>
   <span class="right">
     <i class="icon-add-new"></i>
     Add Translation
   </span>
 <% end %>

triggers this error

ActionView::Template::Error (uninitialized constant Category::Translation):

I looked at the view_helper module and noticed it is this method

def create_object_with_conditions(instance)
      conditions = instance.respond_to?(:conditions) ? instance.conditions.flatten : []
      instance.klass.new(*conditions)
    end

or specifically this line
instance.klass.new(*conditions)

Not sure how to overcome this issue. Seems like it doesnt realize the embeds_many association.

Any assistance is appreciated.

Personally never used it Mongoid, could you explain: is there a Translation class? Should there not be?

What is an embed_many and how would you "create" an instance of a :translation instead?