同じ状況に直面するたびに気になる質問があります。
ネストされたリソースを作成する方法..
次のレポがあります
https://github.com/abhishekdagarit/app-e-commerce.git
リポジトリを複製し、データベースを作成してプロジェクトを表示できます。
製品にカテゴリを追加する必要があります..
product
belongs_to_and_has_many :categories
category
has_many :products
これを適切に機能させる方法を教えてください...個々の製品にコメントを追加しましたが、それを達成するのに4時間かかりました...
これは私が通常行うことです...
1). add the category model using
rails g model category category_type:string
2). then add the has_to and the belongs_to_and_has_many in the models
3). add the controller
rails g controller categories
4). add the following lines in the categories controller
class CategoriesController < ApplicationController
def new
@product = Product.find(params[:product_id])
@category = @product.categories.build
respond_with(@category)
end
def create
@product = Product.find(params[:product_id])
@category = @product.categories.create(params[:category])
redirect_to product_path(@product)
end
end
問題は、これらの手順が機能していないように見えることです...
ネストされたリソースを作成するために機能する数行のコードを手伝ってくれる人が必要です...