Rails Carrierwave White_List 不抛出异常

发布于 2024-10-19 20:57:43 字数 1161 浏览 0 评论 0原文

刚开始将 Carrierwave 与 Rails 一起使用,一切进展顺利,除了一个小例外。我创建了一个“ImageUploader”类,如下所示:

class ImageUploader < CarrierWave::Uploader::Base

include CarrierWave::RMagick
  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

和一个控制器操作,如下所示:

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

在这种情况下,我将上传器安装在我的 User.rb 中,如下所示:

mount_uploader :image, ImageUploader

问题是,根据 Carrierwave README,上传的文件扩展名不在extensions_white_list中应该会使记录无效。就我而言,我特意通过上传不在白名单上的各种扩展名的文件来测试该应用程序,并且没有引发任何错误。事实上,@user.update_attribute 似乎通过了,我通常会被重定向到 user_path(@user) 并带有 flash[:success] 消息。图像本身实际上并未更改,但我希望能够捕获错误并在扩展类型不正确的情况下重定向到“编辑”页面。关于我在这里做错了什么有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-10-26 20:57:43

对于任何感兴趣的人,我自己都找到了解决方法。这不是最优雅的解决方案,但无论如何,这对我有用:

@user.image = params[:user][:image]
if @user.image_integrity_error
    flash[:error] = "Changes could not be saved."
    @user.errors.add(:upload, @user.image_integrity_error)
    render 'edit'
else
    @user.update_attribute(:image, @user.image)
    flash[:success] = "Profile updated successfully!"
    redirect_to user_path(@user)
end

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:

@user.image = params[:user][:image]
if @user.image_integrity_error
    flash[:error] = "Changes could not be saved."
    @user.errors.add(:upload, @user.image_integrity_error)
    render 'edit'
else
    @user.update_attribute(:image, @user.image)
    flash[:success] = "Profile updated successfully!"
    redirect_to user_path(@user)
end
无畏 2024-10-26 20:57:43

仅供参考,CarrierWave 文档声明如下:

Active Record 验证使用 Rails i18n 框架。将这些键添加到您的翻译文件中:

errors:
  messages:
    carrierwave_processing_error: 'Cannot resize image.'
    carrierwave_integrity_error: 'Not an image.'
    carrierwave_download_error: 'Couldn't download image.'

FYI the CarrierWave documentation states the following:

The Active Record validations use the Rails i18n framework. Add these keys to your translations file:

errors:
  messages:
    carrierwave_processing_error: 'Cannot resize image.'
    carrierwave_integrity_error: 'Not an image.'
    carrierwave_download_error: 'Couldn't download image.'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文