2012-08-25 192 views
1

我是rails新手,我开始使用嵌套资源。我一直在阅读http://guides.rubyonrails.org/routing.html#nested-resources,所以我创建了2个模型,一个产品和一个发件人。产品有许多发件人。我的发件人模式是这样的:使用Rails嵌套资源

class Sender < ActiveRecord::Base 
    attr_accessible :product_id, :email, :name 

    belongs_to :product 
end 

我的产品是这

class Product < ActiveRecord::Base 
    attr_accessible :name, :price 

    #Relationships ! 
    has_many :senders, dependent: :destroy 
end 
在我的routes.rb

resources :products do 
    resources :senders 
    end 

现在耙路线给了我应该的没事路线,根据到http://guides.rubyonrails.org/routing.html#nested-resources

所以当我输入网址

http://localhost:3000/products/1/senders/new 

所以我创建了一个新的寄件人我的产品与ID = 1,我得到这样的:

NoMethodError in Senders#new 

undefined method `senders_path' for #<#<Class:0x00000003fcf9d8>:0x00000003e6f408> 

为什么会出现这种不确定的方法,因为它应该给我的new.html.erb页该产品的发件人?

回答

0

请注意,如果您没有rake routes,则找不到要查找的路线。这是因为您的路线senders嵌套在products之内。

因此,如果您要创建一个新的发件人,路径应该是类似new_product_sender_path(product)

因此,要获得特定产品的所有发件人,路径将为product_senders_path(product)