I’m using CarrierWave on my Rails 3 sample app. I want to validate the remote location upload so I don’t get the standard error exception when a user submits an invalid URL either blank or not an image:
CarrierWave::DownloadError in ImageController#create
trying to download a file which is not served over HTTP
This is my model:
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
This is my 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
I’m not really sure how to tackle this problem so any insight would be great.
The solution to this problem has been added to the CarrierWave Wiki on Github.
Edit:
I’m trying to implement the proposed solution now but I can’t get it working. I’m using AR on Rails 3.1.3.
Implementing the code the way it is on the wiki results in the validation actually happening fine. When I try to upload gibberish I get a nice validation message. The problem is that normal uploads are prevented also.