I have the following form declaration for a new kindergarten
<%= form_for @kindergarten, :html => {:multipart => true} do |f|%>
<%= render 'shared/error_messages', object: f.object %>
</br>
<%= f.fields_for :photos do |p| %>
<%= p.label 'upload photo'%>
<%= p.file_field :image %>
<% end %>
</br>
<%= render 'about_company', f: f%>
</br>
<%= render 'contact', f: f %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<%end%>
The logic behind this is that 1 kindergarten can have multiple photos.
Here are the model declarations:
Kindergarten
has_many :photos, limit: 7, dependent: :destroy
accepts_nested_attributes_for :photos
Photo
attr_accessible :image, :logo, :kindergarten_id
belongs_to :kindergarten
mount_uploader :image, ImageUploader
validates :kindergarten_id, presence: true
validates :image, presence: true
And here's how the kindergartens controller looks like:
def new
@kindergarten = Kindergarten.new
@kindergarden.photos.build
end
Now, when @kindergarten new is generated i get the following error:
undefined method 'photos' for nil:NilClass
Application Trace | Framework Trace | Full Trace
app/controllers/kindergartens_controller.rb:5:in `new'