嘿,所以这真的让我很烦。我已经通过简单的图像上传将 Carrierwave 实现到我的应用程序中。我试图验证图像是正确的文件类型,但我得到了一些奇怪的结果。以下是相关代码:
class Art < ActiveRecord::Base
belongs_to :collection
attr_accessible :image, :collection_id, :remote_image_url
mount_uploader :image, ImageUploader
validates_integrity_of :image
validates_presence_of :image
validates_processing_of :image
end
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif)
end
class ArtsController < ApplicationController
def new
@art = Art.new(:collection_id => params[:collection_id])
end
def edit
@art = Art.find(params[:id])
end
def create
@art = Art.new(params[:art])
if @art.save
flash[:notice] = "Successfully created art."
redirect_to @art.collection
else
flash[:error] = @art.errors
render :action => :new
end
end
def update
@art = Art.find(params[:id])
if @art.update_attributes(params[:art])
flash[:notice] = "Successfully updated art."
redirect_to @art.collection
else
flash[:error] = @art.errors
render :action => :edit
end
end
def destroy
@art = Art.find(params[:id])
@art.destroy
flash[:notice] = "Successfully destroyed art."
redirect_to @art.collection
end
end
en:
errors:
messages:
carrierwave_processing_error: "Cannot resize image."
carrierwave_integrity_error: "Not an image."
carrierwave_download_error: "Couldn't download image."
extension_white_list_error: "NOT ALLOWED"
extension_black_list_error: "You are not allowed to upload %{extension} files,
prohibited types: %{prohibited_types}"
因此,当我使用 png 文件格式创建新的艺术品时,会出现图像不能为空白的错误。我知道这是 validates_presence_of 被触发,但我提供的东西格式不正确。如果我删除 validates_presence_of 不正确的文件格式将不会显示,但仍会被创建。我还将 i18n 错误消息添加到我的 en.yml 文件中。我对 Rails 有点陌生,所以我认为我的控制器在创建每件艺术品时存在问题,但我不确定我做错了什么。