在rails 3中使用carrierwave gem验证的问题

时间:2013-10-31 13:49:32

标签: ruby-on-rails-3 validation carrierwave

嘿,这真是让我烦恼。我已经通过简单的图片上传将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

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很新,所以我认为在我的控制器中存在创建每件艺术品的问题,但我不确定我做错了什么。

0 个答案:

没有答案