ruby-on-rails - 为什么回形针上传给出尝试链接错误?

标签 ruby-on-rails ruby rubygems paperclip

所以我有这些文件

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

编辑: 虽然我不认为 application_controller 与错误有任何关系

我正在创建一个包含嵌套图像属性的交易。我正在使用回形针上传图像。但是我收到了这些错误。我什至不知道这些错误是什么意思。这是显示错误的图像。 enter image description here

这是pastebin链接 errors on terminal on creating deal

最佳答案

这似乎是一个验证错误。试试这个来验证:

validates_attachment_content_type :attachment, :content_type => /image/ 

或者对于其他变体,您可以查看 Validate Attachment Content Type Paperclip

在测试您的代码后更新似乎这是一个验证错误,因为 Paperclip 创建了一个图像但不知道 belongs_to 关联。您可以将其设为可选,因为默认情况下 rails 5 需要 belongs_to id 字段。

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

关于ruby-on-rails - 为什么回形针上传给出尝试链接错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584973/

相关文章:

ruby-on-rails - Ruby on Rails - 从具有特定顺序的现有键的散列数组中获取值数组

ruby-on-rails - 在 Ubuntu 10.04 上安装 rails 时出错

ruby-on-rails - 布局中的实例变量

ruby - 上传/发布 stringIO 作为文件

ruby - 树莓派安装SiriProxy : Error on installing Eventmachine 1. 0.0 Ruby Gem

ruby-on-rails - Rails 范围为 null 或为空?

ruby - 循环遍历 XML 以在 Ruby 中创建哈希数组

ruby - 是否有针对巨大的二维数组的搜索算法?

ruby-on-rails - 管理 Rails 的 gem 版本/依赖项

mysql - Win7x64、Ruby 2.0、MySQL 5.6 上的 gem mysql2 段错误