Grundlegendes Routing in Rails 3
-
25-09-2019 - |
Frage
Ich denke, ich denke an alles falsch Routing. Ich habe ein sehr einfaches, zwei Modell-Set-up: Das Produkt und Foto. Artikel has_many: Fotos und Foto belongs_to. Produkt
Produkt hat ein vollständiges Gerüst während Fotos photos_controller hat, dass ich arbeite.
In routes.rb haben wir:
resources :products
(vom Gerüst erzeugt wird)
Als Fotos eine verschachtelte Ressource eines Produkts geändert ich dies:
resources :products do
resources :photos
end
und schließlich:
root :to => "products#index"
Zum Glück Rake Routen ausspuckt:
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"}
Das bedeutet, dass die Form in Produkten / neuen Willen POST Produkte # erstellen, die ich mag dann Fotos von neuen umleiten und ein Formular für die product_photos durch den entsprechenden Fotos / new.html.erb erzeugte das Hochladen von denen POST um Fotos # erstellen, nicht wahr?
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
und in photos_controller.rb (bis jetzt):
def new
@photo = Photo.new
end
Also, warum oh warum oh warum erhalte ich:
Routing Error
No route matches {:controller=>"photos", :action=>"new"}
, wenn Rake Routen klar sagt, ich tun, ich habe eine photos_controller, eine neue Aktion in der photos_controller und new_product_photo_path fragt eindeutig den richtigen Weg zu gehen? (Ich habe auch ein Foto / new.html.erb, die eine einfache <h1>Photos</h1>
hat etwas zu machen).
Ich kann nur feststellen, dass ich über diese alle falsch denke, oder dass ich einen Fehler in der Konvention über Konfiguration gemacht, dass ich nicht sehen kann.
Wer?
Danke und freundliche Grüße, Adam
Lösung
Aktualisiert Antwort:
Ein verschachtelte Ressource Mittel (in diesem Fall), die Sie nur ein neues Foto im Rahmen eines Produkts schaffen. Dies bedeutet, dass die Anwendung wissen muss, welches Produkt das Foto erstellt werden gehört.
Im Fall Ihrer Umleitung Das heißt, Sie das Produkt Objekt als Parameter zu new_product_photo_path
hinzufügen:
redirect_to new_product_photo_path(@product)
Original Antwort:
Das ist, weil Sie es sich um eine verschachtelte Ressource gemacht. /products/1/photos/new
wahrscheinlich funktioniert. Wenn Sie in der Lage sein wollen, auch neue Fotos über /photos/new
zu erstellen, müssen Sie auch eine ‚nicht verschachtelten‘ Ressource hinzufügen.