Reputation: 53
I am new to rails and I started working with nested resources. I have been reading this http://guides.rubyonrails.org/routing.html#nested-resources so I created 2 models, a product and a senders. Product has many Senders. My Sender model is this:
class Sender < ActiveRecord::Base
attr_accessible :product_id, :email, :name
belongs_to :product
end
My product is this
class Product < ActiveRecord::Base
attr_accessible :name, :price
#Relationships !
has_many :senders, dependent: :destroy
end
in my routes.rb:
resources :products do
resources :senders
end
now rake routes gives me all the right routes it should, according to http://guides.rubyonrails.org/routing.html#nested-resources
So when I type in the URL
http://localhost:3000/products/1/senders/new
so I create a new sender for my product with id = 1 I get this:
NoMethodError in Senders#new
undefined method `senders_path' for #<#<Class:0x00000003fcf9d8>:0x00000003e6f408>
Why do I get this undefined method since it should give me the new.html.erb page for the sender of that product??
Upvotes: 1
Views: 120
Reputation: 9691
Notice that if you did rake routes
, you would not find the route you are looking for. This is because your routes for senders
are nested within products
.
Therefore, if you want to create a new sender, the path should be something like new_product_sender_path(product)
.
So, to get all the senders of a particular product, the path would be product_senders_path(product)
.
Upvotes: 0