尝试在我的产品视图中在 Rails 中呈现类别
Posted
技术标签:
【中文标题】尝试在我的产品视图中在 Rails 中呈现类别【英文标题】:Trying to render categories in my product view in rails 【发布时间】:2018-08-14 20:34:45 【问题描述】:我正在尝试在我的产品展示/索引页面上呈现特定产品所属的类别。我不知道我是否需要创建一个连接表,或者我设置模型的方式是否足够好。我已将我的产品模型和控制器关联起来,并为我的类别做了同样的事情。
产品.rb
class Product < ActiveRecord::Base
has_many :orders
belongs_to :categories
validates :name, :price, presence: true
mount_uploader :image, ImageUploader
end
类别.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
【问题讨论】:
【参考方案1】:如果您真正想要的是产品和类别之间的一对一关联,则需要正确设置:
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
【讨论】:
谢谢,我注意到我的代码中除此之外还有一些拼写错误。类别一词显然不是最容易一遍又一遍地键入的。非常感谢。以上是关于尝试在我的产品视图中在 Rails 中呈现类别的主要内容,如果未能解决你的问题,请参考以下文章