嵌套资源表单上的未初始化常量

时间:2013-01-05 05:54:07

标签: ruby-on-rails ruby-on-rails-3

我有三种型号,用户,报告和收据。用户有很多报告,报告有很多收据。

现在,我已设置表单来创建或编辑报告。我需要嵌套另一个表单来创建和编辑收据。我按照rails指南(部分 - 构建一个多模型表单)并编辑了我的模型,并将构建行添加到我的表单视图中,但我得到了“未初始化的常量”错误。

以下是我的模特:

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

和我的表格:

<%# @report.receipts.build %>
<%= form_for([current_user,@report]) do |f| %>
     ...
    <%= f.fields_for ([@report, @report.receipts.build ]) do |receipt| %>
     ...
    <% end %>
<% end %>

我的路线(我不确定我是否应该编辑,但在添加收据资源之前我遇到了同样的错误)

resources :users do
    resources :reports do
        resources :receipts
    end
end

我没有编辑报告控制器,因为rails指南没有显示任何提及它,它的唯一:

def new         @report = current_user.reports.new   端

def编辑         @report = current_user.reports.find(params [:id])   端

我做错了什么?

编辑 - 我更改了收据的表单,因此form_for接收[@report,@ report.receipts.build],但现在我收到错误:

uninitialized constant Report::Receipt

如何让此表单生效?

1 个答案:

答案 0 :(得分:7)

UGH!当我生成模型并给它一个复数名称而不是单数名称时,我搞砸了。这个人,就在这里,是个傻瓜。