views:

25

answers:

1

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

A: 

Updated answer:

Using a nested resource means (in this case) that you can only create a new photo in the context of a product. This means that the application has to know which product the photo to be created belongs to.

In the case of your redirect this means you have to add the product object as a parameter to new_product_photo_path:

redirect_to new_product_photo_path(@product)

Original answer:

This is because you made it a nested resource. /products/1/photos/new probably does work. If you want to be able to create new photos via /photos/new too, you have to add an 'unnested' resource too.

captaintokyo
Thank you for your answer Cap'n T. I did initially have :photos as an unnested resource and it worked fine. My question really is to get a better understanding of routes in rails (and maybe URLs and RESTful design), rather than to solve a problem as such. Conceptually, photos are a nested resource of products and rake routes says that new_product_photo should lead me to the new action inside the photos_controller. I am not specifying anywhere that that is photos/new nor /products/1/photos/new, I am using a controller to redirect to new_product_photo and can't see *why* it doesn't work.
Adam
Ah, sorry I didn't read your question carefully enough. I will update my answer.
captaintokyo
That makes a lot more sense conceptually. Thanks very much for taking the time to answer it. Trying it out gives the expected redirect (Redirected to http://localhost:3000/products/27/photos/new) but fails with an HTTP (I think) error (Completed 406 Not Acceptable in 85ms). Any ideas? Cutting 'n' pasting /products/27/photos/new into the address bar gets me to the right place though, so different issue?
Adam
Not too worry - had an extra 'end' in my action while butchering the scaffold-generated controller. Thanks again captaintokyo.
Adam