Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
boyfunky opened this issue Jul 8, 2020 · 1 comment

Comments

@boyfunky
Copy link

boyfunky commented Jul 8, 2020

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.

@nathanvda
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants