简体   繁体   中英

Trying to render categories in my product view in rails

I'm trying to render on my product show/index page what category a specific product belongs to. I don't know if I need to create a join table or if the way that I have set up the models is good enough. I've linked my product model and controllers, and have done the same for my categories.

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

If what you really want is a one-to-one association between products and categories you need to set it up properly:

class Product < ActiveRecord::Base
  belongs_to :category
  # ...
end

class Category < ActiveRecord::Base
  has_many :products
  # ...
end

Proper pluralization is really important in Rails.

Its only you want to instead have a many to many association (a product can belong to many categories) that you need a join table:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM