Accepts_nested_attributes_for and fields_for not working!

I want to be able to create one menu object that has fields_for :brunch,
:lunch, and :dinner. The menu object has_many brunches, lunches, and
dinners, but each time I “edit”, only one brunch, one lunch, and one
dinner
object are added. I’m using carrierwave to upload brunch_menus,
lunch_menus, and dinner_menus (attributes of brunches, lunches, and
dinners
respectively) as pdfs to amazon s3 and viewing them as iframes. When I
try
to save a menu object, the menu object gets created and saved, but its
brunch, lunch and dinner don’t get created when I edit it. When i run
Brunch.all i get []

Thanks!
Sarah

Menu Controller:
before_filter :authenticate_admin!, :except => [:show]
def edit
@menu = Menu.find(params[:id])
@menu.dinners.build
@menu.lunches.build
@menu.brunches.build
end

Menu Model:
class Menu < ActiveRecord::Base
has_many :brunches, :dependent => :destroy
has_many :lunches, :dependent => :destroy
has_many :dinners, :dependent => :destroy
accepts_nested_attributes_for :lunches, :dinners, :brunches
end

Brunch, lunch, dinner models:
class Lunch < ActiveRecord::Base
attr_accessible :menu_id, :lunch_menu
belongs_to :menu
mount_uploader :lunch_menu, ImageUploader
end

Menu Form:
<%= form_for @menu, :html => {:multipart => true} do |f| %>
<% if @menu.errors.any? %>


<%= pluralize(@menu.errors.count, “error”) %> prohibited this
menu from being saved:

  <ul>
  <% @menu.errors.full_messages.each do |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>

<% end %>
<%= f.fields_for :brunches do |a|%>

<%= a.label :brunch_menu %>
<%= a.file_field :brunch_menu %>
<% end %> <%= f.fields_for :lunches do |a|%>
<%= a.label :lunch_menu %>
<%= a.file_field :lunch_menu %>
<% end %> <%= f.fields_for :dinners do |a|%>
<%= a.label :dinner_menu %>
<%= a.file_field :dinner_menu %>
<% end %>
<%= f.submit %>
<% end %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs