载波更新用户头像而不在参数中发送头像

时间:2019-01-08 23:31:47

标签: ruby-on-rails carrierwave

我有一个前端应用程序和一个Rails后端。我在后端有一个AvatarUploader来上传文件,当我从UI发送文件时,它可以很好地上传文件。但是,当我不从帐户更新表单发送文件时,用户头像就会更新。我可以确认我没有从表单发送图像:

puts params
{..., "avatar"=>nil, ... }
但是在日志中我可以看到sql UPDATE
UPDATE "users" SET "avatar" = $1, "updated_at" = $2 WHERE "users"."id" = $3  [["avatar", "a36ed6b7-c207-44e8-9e93-6cfc3eaeed86.jpg"], ["updated_at", "2019-01-08 23:18:50.140517"], ["id", 4]]

这是我的AvatarUploader

class AvatarUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  version :avatar_m do
    process resize_to_fill: [226, 150]
  end

  version :avatar_s, from_version: :avatar do
    process resize_to_fill: [48, 48]
  end

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

  def extension_whitelist
    %w(jpg jpeg gif png)
  end

  def size_range
    1..10.megabytes
  end

  def default_url(*args)
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end

  def filename
    "#{secure_token}.#{file.extension}" if original_filename.present?
  end

  private

  def env_path
    env = Rails.env.test? ? "/spec" : ""
    "uploads#{env}"
  end

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) || model.instance_variable_set(var, SecureRandom.uuid)
  end
end
我很困惑...当您未按更新请求发送文件时,Carrierrwave是否会更新模型?

0 个答案:

没有答案