我试图在我的产品展示/索引页面上呈现特定产品所属的类别。我不知道是否需要创建联接表,或者我建立模型的方式是否足够好。我已经链接了我的产品模型和控制器,并且对我的类别也做了同样的事情。
Product.rb
class Product < ActiveRecord::Base
has_many :orders
belongs_to :categories
validates :name, :price, presence: true
mount_uploader :image, ImageUploader
end
Categories.rb
class Category < ActiveRecord::Base
has_many :products
has_many :users, through: :categories
validates :name, presence: true
TITLE = %w{ Electronics Home Garden Lifestyle }
end
Categories_controller.rb
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
def index
@categories = Category.all
@products = Product.all.order("created_at desc")
end
def show
@products = Product.where("category_id = ?", @product.id)
@categories = Category.all
end
def new
@catgories = Category.new
end
def create
@category = Category.new(category_params)
respond_to do |format|
if @category.save(category_params)
format.html { redirect_to categories_path, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html {render :new, notice: "Category failed to be created" }
format.json { render json: @category.errors, status: :unprocessable_entity}
end
end
end
def electronics
end
def home
end
def update
@category = Category.new(category_params)
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to categories_path, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html {render :edit, notice: "Category failed to be updated" }
format.json { render json: @category.errors, status: :unprocessable_entity}
end
end
end
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to categories_path, notice: "Category has been deleted" }
format.json { head :no_content }
end
end
private
def set_category
@category = Category.find(params[:id])
end
def category_params
params.require(:category).permit(:title)
end
end
products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_action :category_order, only: [:index, :show, :edit, :new]
before_action :products_order, only: [:index, :show]
before_action :authenticate_user!, except: [:index, :show]
# GET /products
# GET /products.json
def index
@products = Product.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new, notice: "Product could not be saved" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit, notice:"Product could not be updated" }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:name, :price, :image, :category_id, :description)
end
def products_order
@products = Product.all.order("created_at desc")
end
#category
def category_order
@categories = Category.all.order("created_at desc")
end
end
答案 0 :(得分:0)
如果您真正想要的是产品和类别之间的一对一关联,则需要正确设置:
class Product < ActiveRecord::Base
belongs_to :category
# ...
end
class Category < ActiveRecord::Base
has_many :products
# ...
end
在Rails中,正确的多元化非常重要。
只有您想要建立一个多对多关联(一个产品可以属于许多类别),而您需要一个联接表:
class Product < ActiveRecord::Base
has_many :product_categories
has_many :categories, through: :product_categories
# ...
end
# the join table
class ProductCategory < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
class Category < ActiveRecord::Base
has_many :product_categories
has_many :products, through: :product_categories
# ...
end