嵌套资源路由问题

时间:2012-12-27 21:28:25

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

这可能很简单,但我在一段时间内没有弄乱轨道而且我不确定自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

是什么给出了?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

不应该是:

new_report_receipts_path

?注意s,即

  

link_to'New Receipt',new_report_receipts_path,:class =&gt;“btn   BTN-成功“