Just started using carrierwave with Rails and things have been going smoothly with one minor exception. I created a “ImageUploader” class which looks so:
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
def extension_white_list
%w(jpg jpeg gif png)
end
end
and a controller action which looks like so:
def update
@user = current_user
if params[:user].nil? || params[:user][:image].nil?
redirect_to user_path(@user)
else
if @user.update_attribute(:image, params[:user][:image])
flash[:success] = "Profile updated successfully!"
redirect_to user_path(@user)
else
flash[:error] = "Changes could not be saved."
render :action => 'edit'
end
end
end
In this case I mounted the uploader in my User.rb like so:
mount_uploader :image, ImageUploader
Problem is, according to the Carrierwave README, uploaded files with extensions not in the extensions_white_list should make the record invalid. In my case I have purposely been testing the app by uploading files with various extensions not on the white list and no error is being raised. In fact, @user.update_attribute seems to pass and I am usually redirected to user_path(@user) with a flash[:success] message. The image itself is not actually changed, but I would like to be able to catch the error and redirect to the ‘edit’ page in case of an incorrect extension type. Any ideas on what I am doing wrong here?
For anyone interested I sort of found a workaround for this myself. Not the most elegant solution, but in any case here’s what worked for me: