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
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
因此,当我使用png文件格式创建一件新艺术品时,将出现图像不能为空的错误。我知道这是触发器的validates_presence_of,但我提供的东西不是正确的格式。如果我删除validates_presence_of非正确的文件格式将不会显示但仍将创建。我还把i18n错误消息写到我的en.yml文件中。我对rails很新,所以我认为在我的控制器中存在创建每件艺术品的问题,但我不确定我做错了什么。