asdfkjasdfjk
asdfkjasdfjk

Reputation: 3904

undefined local variable or method `maindata_form' in rails 4

My two models

Maindata.rb

has_many :applications, :dependent => :destroy, :autosave => true
accepts_nested_attributes_for :applications

Application.rb

belongs_to :maindata
accepts_nested_attributes_for :maindata

And in the form to save maindata I render application partial as well

<%= form_for @main_data do |maindata_form| %>
  <% 3.times { @maindata.applications.build } %>  
  <%= maindata_form.fields_for :applications do |builder| %>                   
    <%= render 'application', maindata_form: builder %>    
  <% end %>
<% end %>

and application partial

      <div>
        <%= maindata_form.label :uni_id, "University" %> ***
        <%= maindata_form.collection_select :uni_id, @unis, :id, :bezeichnung, {:include_blank => true} %>
      </div>

When I load the form I get error at the star marked line as

undefined local variable or method `maindata_form' for #<#:0x8451588>

Is this a syntax error or am I missing something?

Upvotes: 0

Views: 76

Answers (2)

Yuri H.
Yuri H.

Reputation: 51

You should call render with :render and :locals parameters:

<%= render partial: 'application', locals: { maindata_form: builder } %> 

Upvotes: 0

kalelc
kalelc

Reputation: 1527

I assume you're trying to create a form from the model. In this case you should:

First: Assign the variable in the controller method @main_data = Maindata.new

Second: This variable link it to the form, for example:

<%= form_for @main_data do |f| %>
<div>
<%= f.label :uni_id, "University" %> ***
<%= f.collection_select :uni_id, @unis, :id, :bezeichnung, {:include_blank => true} %>
</div>
<% end %>

Upvotes: 0

Related Questions