CarrierWave事务回滚

时间:2017-03-14 04:46:11

标签: ruby-on-rails ruby google-cloud-platform carrierwave fog

我一直在尝试连接Carrierwave进行图片上传,使用雾和谷歌云平台进行存储。但是,每次尝试通过users#update访问数据库时,我的基本实现都会回滚。我没有成功地解决这个问题,任何帮助都非常感谢。 Rails版本为5.0.1

**我已经退回到使用carrierwave的本地存储来尝试调试此问题,但我仍然遇到相同的回滚问题。我已更新文件以反映这一点。

**更新:活动记录模型提供的错误消息为Avatar translation missing: en.errors.messages.mini_magick_processing_error

的Gemfile

...
gem 'carrierwave', '~> 1.0'
gem 'mini_magick'

avatar_uploader.rb:

class AvatarUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :display do
    process :resize_to_fill => [200, 200]
  end

  version :thumb do
    process :resize_to_fit => [80, 80]
  end

end

users_controller.rb

class UsersController < ApplicationController
  before_action :authenticate_user!, only: [:edit, :update]
  before_action :check_authorization, only: [:edit, :update]
  before_action :set_user

  def show
  end

  def edit
  end

  def update
    if @user.update(user_params)
      redirect_to @user
    else
      flash.now[:notice] = "Something went wrong. Please try again."
      render :edit
    end
  end

  private
    def check_authorization
      unless current_user.id == params[:id].to_i
        redirect_to root_url
      end
    end

    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      params.require(:user).permit(:avatar)
    end

end

模型/ user.rb

class User < ApplicationRecord

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_and_belongs_to_many :meetings
  has_many :action_items
  has_many :tasks
  has_many :comments

  mount_uploader :avatar, AvatarUploader

end

edit.html.erb

<h1>Edit Profile</h1>

<%= form_for(@user, html: {multipart: true}) do |f| %>

  <%= f.file_field :avatar %>

  <%= f.submit "Save Change" %>

<% end %>

enter image description here

2 个答案:

答案 0 :(得分:2)

我的Imagemagick安装出现问题。我重新安装,问题得到解决。

答案 1 :(得分:0)

请在AvatarUploader中包含以下行并重新启动服务器

def extension_white_list
  %w(jpg jpeg gif png)
end

我引用Post

中的以下edudepetris