I have three models, users, reports, and receipts. Users have many reports, and reports have many receipts.
Now, I have a form set up to create, or edit reports. And I need to nest another form to create and edit receipts. I followed the rails guide (section – building a multi modeled form) and edited my models, and have added the build line into my form view but Im getting that ‘uninitialized constant’ error.
Here are my models:
class Report < ActiveRecord::Base
belongs_to :user
has_many :receipts
attr_accessible :cash_advance, :company, :description, :end_date, :mileage, :report_name,
:start_date, :receipts_attributes
validates_presence_of :company, :description, :end_date, :report_name#, :start_date
validates_uniqueness_of :report_name
accepts_nested_attributes_for :receipts, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
class Receipts < ActiveRecord::Base
belongs_to :report
attr_accessible :account_code, :amount, :company_card, :date, :description, :lobbying_expense, :vendor
end
and my form:
<%# @report.receipts.build %>
<%= form_for([current_user,@report]) do |f| %>
...
<%= f.fields_for ([@report, @report.receipts.build ]) do |receipt| %>
...
<% end %>
<% end %>
my routes (which Im not sure if I should have edited, but I got the same error before I added the receipts resources)
resources :users do
resources :reports do
resources :receipts
end
end
I didnt edit the reports controller since the rails guide didnt show any mention of it, its its only:
def new
@report = current_user.reports.new
end
def edit
@report = current_user.reports.find(params[:id])
end
What am I doing wrong?
edit – I changed my form for the receipts so the form_for takes in [@report, @report.receipts.build] but now I get the error:
uninitialized constant Report::Receipt
How do I get this form to work?
UGH! I messed up when I generated the model and gave it a plural name instead of a singular name. This guy, right here, is a fool.