문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top