3

So I have these files

deal.rb

class Deal < ApplicationRecord
  has_many :images, as: :imageable, dependent: :destroy
  #there is more code after this
end

image.rb

class Image < ApplicationRecord
  belongs_to :imageable, polymorphic: true
  belongs_to :deal
  has_attached_file :attachment, styles:  { thumb: "100x100!", medium: "200x200!" }
  validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
end

deals_controller.rb

module Admins
  class DealsController < BaseController
    before_action :find_deal, only: [:edit, :update, :destroy]

    def index
      @deals = Deal.includes(:images)
    end

    def new
      @deal  = Deal.new
    end

    def edit
    end

    def create
      @deal = Deal.new(deal_params.merge(created_by: current_user.id))
      if @deal.save
        flash[:success] = t('.success')
        redirect_to admins_deals_url
      else
        flash.now[:warning] = t('.failure')
        render :new
      end
    end

   def update
    if @deal.update(deal_params)
      flash[:success] = t('.success')
      redirect_to admins_deals_url
    else
      flash.now[:warning] = @deal.errors[:base].to_sentence
      render :edit
    end
   end

   def destroy
     if @deal.destroy
       flash[:success] = t('.success')
       redirect_to admins_deals_url
     else
       flash.now[:warning] = t('.failure')
       render :index
     end
   end

  private
   def deal_params
     params.require(:deal).permit(:title, :description, :price, :discounted_price, :quantity, :publish_date, images_attributes: [:id, :attachment, :_destroy])
   end

   def find_deal
     @deal = Deal.find_by(id: params[:id])
     unless @deal
       flash[:warning] = t('deals.not_found')
       redirect_to admins_deals_path
     end
   end
 end
end

application_controller.rb

class ApplicationController < ActionController::Base
  helper_method :current_user, :current_cart

  def current_user
    @current_user ||= User.find_by(id: current_user_id)
  end

  def current_user_id
    cookies.signed[:user_id] || session[:user_id]
  end

  def current_cart
    @current_cart ||= (current_user.addressed_cart || current_user.cart) if current_user
  end
end

EDIT: Although I don't think application_controller has anything to do with the error

I am creating a deal with nested image attributes. I am using paperclip to upload the images. But I am getting these errors. I don't have any idea what the errors even mean. Here is an image to show the errors.enter image description here

Here is the pastebin link errors on terminal on creating deal

Jayanti Hari
  • 239
  • 3
  • 11

1 Answers1

4

This appears to be a validation error. Try this for your validation:

validates_attachment_content_type :attachment, :content_type => /image/ 

Or for other variations you can see Validate Attachment Content Type Paperclip

UPDATE after testing your code seems this was a validation error because Paperclip creates an image but doesn't know about the belongs_to association. You can make it optional because by default rails 5 requires the belongs_to id field.

class Image < ApplicationRecord
  belongs_to :imageable, polymorphic: true
  belongs_to :deal, optional: true
  has_attached_file :attachment, styles:  { thumb: "100x100!", medium: "200x200!" }
  validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
end
lacostenycoder
  • 10,623
  • 4
  • 31
  • 48