0

这可能很简单,但是我有一段时间没有弄乱rails了,我不确定自3.2以来情况是否发生了很大变化

所以我有两个模型,报告和收据。每份报告都有很多收据,所以我的模型看起来像:

class Report < ActiveRecord::Base

    has_many :receipts, :dependent => :destroy
  accepts_nested_attributes_for :receipts, :allow_destroy => true

    attr_protected :id

end
class Receipt < ActiveRecord::Base
    belongs_to :report

    attr_protected :id

    validates_presence_of :vendor, :date, :description, :amount, :acctCode
end

我的收据控制器位于控制器文件夹的根目录中,并列出了所有操作(它是由脚手架生成的)

我的路线有:

root :to => "index#index"

resources :reports do
    resources :receipts
end

我的 link_to 看起来像这样:

<%= link_to 'New Receipt', new_report_receipt_path, :class=>"btn btn-success" %>

这给了我错误:

No route matches {:action=>"new", :controller=>"receipts"}

我跑了 rake 路线,我得到了以下信息:

     report_receipts GET    /reports/:report_id/receipts(.:format)          receipts#index
                     POST   /reports/:report_id/receipts(.:format)          receipts#create
  new_report_receipt GET    /reports/:report_id/receipts/new(.:format)      receipts#new
 edit_report_receipt GET    /reports/:report_id/receipts/:id/edit(.:format) receipts#edit
      report_receipt GET    /reports/:report_id/receipts/:id(.:format)      receipts#show
                     PUT    /reports/:report_id/receipts/:id(.:format)      receipts#update
                     DELETE /reports/:report_id/receipts/:id(.:format)      receipts#destroy

是什么赋予了?

4

2 回答 2

1

您只是忘记在您的路线中提供报告 ID。这取决于您如何在视图中访问您的报告,但它必须类似于: new_report_receipt_path(@report)

于 2012-12-27T21:34:15.490 回答
0

那不应该是:

new_report_receipts_path

? 注意s,即

link_to '新收据',new_report_receipts_path,:class=>"btn btn-success"

于 2012-12-27T21:31:26.970 回答