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

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

这是我的 Controller :
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

我不太确定如何解决此问题,因此任何见解都会很棒。

最佳答案

这个问题的解决方案已添加到Github上的CarrierWave Wiki中。

编辑:
我正在尝试实现建议的解决方案,但无法正常工作。我在Rails 3.1.3上使用AR。

以wiki上的方式实现代码可以使验证实际上进行得很好。当我尝试上传乱码时,会收到一条不错的验证消息。问题在于,正常上传也被阻止。

关于ruby-on-rails - 我该怎么办:remote location validation with CarrierWave?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6012677/

10-16 05:41