向rails中的帖子添加标签时出错

时间:2017-04-14 13:18:38

标签: ruby-on-rails

我想在rails项目中为我的产品添加标签,所以我观看了YouTube视频如何操作(https://www.youtube.com/watch?v=rzx5MrCa0Pc&t=254s) 我做了他所做的一切,但是当我添加新产品时,我收到了错误 - '新产品 1错误禁止此产品被保存: 用户必须存在' ,就在我的新产品表格之上 我该如何解决呢。

我的路线

Rails.application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations"}
resources :products
        get 'home/ContactUs'
    get 'home/Login'
get 'home/Store'
get 'home/blogs'
get 'home/index'
    resources :home
  root 'home#index'

我的产品型号

class Product < ActiveRecord::Base
    belongs_to :user
    has_many :taggings, dependent: :destroy
    has_many :tags, through: :taggings

    def self.tagged_with(name)
        Tag.find_by!(name: name).products
    end

    def all_tags=(names)
        # names="music, spotify"
        self.tags = names.split(',').map do |name|
            Tag.where(name: name).first_or_create!
        end
    end
    def all_tags
        tags.map(&:name).join(", ")
    end
end

标签模型

class Tag < ApplicationRecord
    has_many :taggings, dependent: :destroy
    has_many :products, through: :taggings
end

TAGGINGS MODEL

class Tagging < ActiveRecord::Base
  belongs_to :product
  belongs_to :tag
end

用户模型

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

         has_many :products
end

PRODUCT_CONTROLLER

class ProductsController < ApplicationController
  # before_action :authenticate_user!
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
     if params[:tag]
    @products = Product.tagged_with(params[:tag])
  else
    @products = Product.all
  end
  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 }
        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 }
        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(:filetype, :title, :img_url, :description, :all_tags, :price, :uploaded_by, :tutorial_url)
    end
end

1 个答案:

答案 0 :(得分:1)

  

用户必须存在

问题是Product属于User,但您尝试创建的产品没有通过user_id来创建孤儿产品

解决方案:更改

def create
  @product = Product.new(product_params)

def create
  @product = current_user.products.new(product_params)

此外,您需要更改set_product方法,以确保只能updatedelete用户创建的产品

def set_product
  @product = current_user.products.find(params[:id])
end