Hi all
I think I am thinking about routing all wrong. I have a very simple, two model set-up: Product and Photo. Product has_many :photos, and Photo belongs_to :product.
Product has a full scaffold while Photo has a photos_controller that I am working on.
In routes.rb we have:
resources :products
(generated by the scaffold)
As photos are a nested resource of a product I changed this to:
resources :products do
resources :photos
end
and finally:
root :to => "products#index"
Happily rake routes spits out:
products GET {:controller=>"products", :action=>"index"}
products POST {:controller=>"products", :action=>"create"}
new_product GET {:controller=>"products", :action=>"new"}
edit_product GET {:controller=>"products", :action=>"edit"}
product GET {:controller=>"products", :action=>"show"}
product PUT {:controller=>"products", :action=>"update"}
product DELETE {:controller=>"products", :action=>"destroy"}
product_photos GET {:controller=>"photos", :action=>"index"}
product_photos POST {:controller=>"photos", :action=>"create"}
new_product_photo GET {:controller=>"photos", :action=>"new"}
edit_product_photo GET {:controller=>"photos", :action=>"edit"}
product_photo GET {:controller=>"photos", :action=>"show"}
product_photo PUT {:controller=>"photos", :action=>"update"}
product_photo DELETE {:controller=>"photos", :action=>"destroy"}
products GET {:controller=>"products", :action=>"index"}
products POST {:controller=>"products", :action=>"create"}
new_product GET {:controller=>"products", :action=>"new"}
edit_product GET {:controller=>"products", :action=>"edit"}
product GET {:controller=>"products", :action=>"show"}
product PUT {:controller=>"products", :action=>"update"}
product DELETE {:controller=>"products", :action=>"destroy"}
root {:controller=>"products", :action=>"index"}
which means that the form in products/new will POST to products#create which I want to then redirect to photos#new and have a form for uploading the product_photos generated by the corresponding photos/new.html.erb which will POST to photos#create, right?
in product_controller.rb:
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
redirect_to new_product_photo_path, :notice => 'Product was successfully created.'
else
render :action => "new"
end
end
end
and in photos_controller.rb (for now):
def new
@photo = Photo.new
end
So why oh why oh why do I get:
Routing Error
No route matches {:controller=>"photos", :action=>"new"}
when rake routes clearly says I do, I have a photos_controller, a new action in the photos_controller, and new_product_photo_path is clearly asking to go the right way? (I also have a photos/new.html.erb that has a simple <h1>Photos</h1>
for something to render).
I can only conclude that I am thinking about this all the wrong way, or that I have made an error in Convention over Configuration that I can't see.
Anybody?
Thanks and Kind Regards, Adam