我该怎么做:使用CarrierWave进行远程位置验证?

时间:2011-05-16 02:27:37

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

我在我的Rails 3示例应用程序上使用CarrierWave。我想验证远程位置上传,因此当用户提交无效的URL空白或不是图像时,我不会收到标准错误异常:

CarrierWave::DownloadError in ImageController#create
trying to download a file which is not served over HTTP

这是我的模特:

class Painting < ActiveRecord::Base
  attr_accessible :gallery_id, :name, :image, :remote_image_url
  belongs_to :gallery
  mount_uploader :image, ImageUploader

  validates :name,        :presence => true,
                          :length =>  { :minimum => 5, :maximum => 100 }
  validates :image,       :presence => true

end

这是我的控制者:

class PaintingsController < ApplicationController
  def new
    @painting = Painting.new(:gallery_id => params[:gallery_id])
  end

  def create
    @painting = Painting.new(params[:painting])
    if @painting.save
      flash[:notice] = "Successfully created painting."
      redirect_to @painting.gallery
    else
      render :action => 'new'
    end
  end

  def edit
    @painting = Painting.find(params[:id])
  end

  def update
    @painting = Painting.find(params[:id])
    if @painting.update_attributes(params[:painting])
      flash[:notice] = "Successfully updated painting."
      redirect_to @painting.gallery
    else
      render :action => 'edit'
    end
  end

  def destroy
    @painting = Painting.find(params[:id])
    @painting.destroy
    flash[:notice] = "Successfully destroyed painting."
    redirect_to @painting.gallery
  end
end

我不确定如何解决这个问题所以任何见解都会很棒。

4 个答案:

答案 0 :(得分:8)

我遇到了同样的问题。不幸的是,这看起来像是CarrierWave的设计缺陷......它不允许对远程URL进行适当的验证。 当属性设置时,CarrierWave将立即尝试下载资源,如果url无效,无法访问或资源没有预期类型,则会抛出异常。 在进行任何验证之前,始终会抛出DownloadError或IntegrityErrors。

因此我找不到使用其他验证器的好方法。我的解决方案最终看起来像这样:

valid = false
begin
  par = params[:image].except(:remote_upload_url)
  @image = Image.new(par)
  # this may fail:
  @image.remote_upload_url = params[:image][:remote_upload_url]
  valid = true
rescue CarrierWave::DownloadError
  @image.errors.add(:remote_upload_url, "This url doesn't appear to be valid")
rescue CarrierWave::IntegrityError
  @image.errors.add(:remote_upload_url, "This url does not appear to point to a valid image")
end 

# validate and save if no exceptions were thrown above
if valid && @image.save
  redirect_to(images_configure_path)
else
 render :action => 'new'
end

基本上,我将构造函数包装在一个急救块中,并初始设置除远程URL之外的所有参数。当我设置它时,可能会发生异常,我通过手动设置模型中的错误来处理。请注意,在此方案中不执行其他验证。这是一个黑客,但为我工作。

我希望通过将资源下载延迟到模型验证阶段或之后,可以在将来的版本中解决这个问题。

答案 1 :(得分:1)

这是非常烦人的问题。我暂时在我的rescue_from中执行了application_controller.rb,只是显示了问题的Flash消息。这是我能想到的最好的。如果您有多个需要这些验证的模型,我不会成为堵塞控制器并不得不使用重复代码的粉丝。

  rescue_from CarrierWave::DownloadError, :with => :carrierwave_download_error
  rescue_from CarrierWave::IntegrityError, :with => :carrierwave_integrity_error

  def carrierwave_download_error
    flash[:error] = "There was an error trying to download that remote file for upload. Please try again or download to your computer first."
    redirect_to :back
  end

  def carrierwave_integrity_error
    flash[:error] = "There was an error with that remote file for upload. It seems it's not a valid file."
    redirect_to :back
  end

答案 2 :(得分:0)

此问题的解决方案已添加到Github上的CarrierWave Wiki

修改
我正在尝试实施现在提出的解决方案,但我无法让它工作。我在Rails 3.1.3上使用AR。

按照wiki上的方式实现代码会导致验证确实正常。当我尝试上传乱码时,我得到一个很好的验证消息。问题是也可以防止正常上传。

答案 3 :(得分:0)

CarrierWave wiki上的解决方案对我不起作用。正如Peter Hulst所说,CarrierWave在验证之前加载文件。我通过在抛出异常时捕获异常并稍后将其作为验证错误添加回来找到了解决此问题的方法。出于某种原因,当抛出异常时,所有其他记录的属性都变为零,因此在验证之前还必须捕获并重新添加它们。这段代码都在您的模型中。

使用配置中的错误消息而不是硬编码仍然需要稍微修改。

attr_accessor :additional_error_message, :original_attributes

def initialize(*args)
  self.original_attributes = args[0]
  begin
    super
  rescue CarrierWave::IntegrityError # bad file type
    self.additional_error_message = 'must be a PNG, JPEG, or GIF file' # depends on your whitelist
  rescue OpenURI::HTTPError # 404
    self.additional_error_message = 'could not be found'
  rescue RuntimeError # redirection
    self.additional_error_message = 'could not be loaded'
  rescue CarrierWave::DownloadError
    self.additional_error_message = 'could not be loaded'
  rescue
    self.additional_error_message = 'could not be loaded'
  end
end

before_validation do |image|
  if additional_error_message.present?
    errors.add(remote_image_url, additional_error_message)
    self.name = original_attributes[:name] # replace this with re-adding all of your original attributes other than the remote_image_url
  end
end

# the image will have an "is blank" error, this removes that
after_validation do |image|
  errors.delete(:image) if additional_error_message.present?
end