فهم التوجيه في القضبان 3
-
25-09-2019 - |
سؤال
أعتقد أنني أفكر في توجيه كل خطأ. لديّ طراز بسيط للغاية: المنتج والصور. المنتج HAS_MANY: الصور ، والصورة تنتمي: المنتج.
يحتوي المنتج على سقالة كاملة بينما يحتوي Photo على صور _controller أعمل عليه.
في Routes.RB لدينا: resources :products
(الناتجة عن السقالة)
نظرًا لأن الصور مورد متداخل لمنتج قمت بتغيير هذا إلى:
resources :products do
resources :photos
end
وأخيرا:
root :to => "products#index"
لحسن الحظ ، تبصق طرق أشعل النار:
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"}
مما يعني أن النموذج في المنتجات/new سوف ينشر إلى المنتجات#إنشاء ما أريد إعادة توجيهه إلى الصور#جديدة ولديه نموذج لتحميل Product_photos الذي تم إنشاؤه بواسطة الصور المقابلة/new.html.erb التي ستنشر إلى الصور# خلق ، أليس كذلك؟
في 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
وفي Photos_Controller.rb (الآن):
def new
@photo = Photo.new
end
فلماذا يا لماذا أحصل:
Routing Error
No route matches {:controller=>"photos", :action=>"new"}
عندما تقول Rake Routes بوضوح أنني أفعل ذلك ، لدي صور _controller ، وإجراء جديد في Photo_Controller ، و New_product_photo_path يطلب بوضوح الذهاب بالطريقة الصحيحة؟ (لدي أيضًا صور/new.html.erb التي تحتوي على بسيطة <h1>Photos</h1>
لشيء لتقديمه).
لا يسعني إلا أن أستنتج أنني أفكر في هذا الأمر بالطريقة الخاطئة ، أو أنني ارتكبت خطأً في الاتفاقية على التكوين لا يمكنني رؤيته.
اي شخص؟
شكرا ومع تحيات طيبة ، آدم
المحلول
الجواب المحدث:
باستخدام مورد متداخل (في هذه الحالة) يمكنك فقط إنشاء صورة جديدة في سياق المنتج. هذا يعني أن التطبيق يجب أن يعرف المنتج الذي ينتمي إليه الصورة التي سيتم إنشاؤها.
في حالة إعادة توجيهك ، هذا يعني أنه يتعين عليك إضافة كائن المنتج كمعلمة إلى new_product_photo_path
:
redirect_to new_product_photo_path(@product)
الإجابة الأصلية:
هذا لأنك جعلته مورد متداخل. /products/1/photos/new
ربما يعمل. إذا كنت تريد أن تكون قادرًا على إنشاء صور جديدة عبر /photos/new
أيضًا ، يجب عليك إضافة مورد "غير مهتم" أيضًا.