error while adding tags to a post in rails -


i wanted add tags products in rails project, watched youtube video how (https://www.youtube.com/watch?v=rzx5mrca0pc&t=254s) did did, when add new product error - 'new product 1 error prohibited product being saved: user must exist' , right above new product form how fix it.

my routes

rails.application.routes.draw devise_for :users, :controllers => { :registrations => "registrations"} resources :products         'home/contactus'     'home/login' 'home/store' 'home/blogs' 'home/index'     resources :home   root 'home#index' 

my product model

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 |name|             tag.where(name: name).first_or_create!         end     end     def all_tags         tags.map(&:name).join(", ")     end end 

tag model

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 

user model

class user < activerecord::base   # include default devise modules. others available are:   # :confirmable, :lockable, :timeoutable , :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]    # /products   # /products.json   def index      if params[:tag]     @products = product.tagged_with(params[:tag])   else     @products = product.all   end   end    # /products/1   # /products/1.json   def show   end    # /products/new   def new     @product = product.new   end    # /products/1/edit   def edit   end    # post /products   # post /products.json   def create     @product = product.new(product_params)      respond_to |format|       if @product.save         format.html { redirect_to @product, notice: 'product 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 |format|       if @product.update(product_params)         format.html { redirect_to @product, notice: 'product 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 |format|       format.html { redirect_to products_url, notice: 'product destroyed.' }       format.json { head :no_content }     end   end    private     # use callbacks share common setup or constraints between actions.     def set_product       @product = product.find(params[:id])     end      # never trust parameters scary internet, allow white list through.     def product_params       params.require(:product).permit(:filetype, :title, :img_url, :description, :all_tags, :price, :uploaded_by, :tutorial_url)     end end 

user must exist

the problem product belongs user trying create product without passing user_id create orphan product

solution: change

def create   @product = product.new(product_params) 

to

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

also, need change set_product method make sure can update or delete products created user

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

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -