Search code examples
ruby-on-railsfilecontrollercarrierwaveuploader

carrierwave doesn't save file neither update model


Hi I'm using carrierwave in my rails api to upload users avatars ... In my controller i have a method update_avatar that update user.avatar_uri with the given file.

I looked up carrierwave's documentation and followed every step, but it's not working. It doesn't save the file neither the model itself ..

Here's the controller users_controller.rb

    def update_avatar
    parameters = Hash.new
    user = User.find_by(id: params[:user_id])
    if user
      #p("----user.avatar_uri.url before user.avatar_uri = params[:avatar] : "+user.avatar_uri.url)

      user.is_used_fb_avatar=false
      p("----user.avatar_uri.url before update: "+ user.avatar_uri.url)
      User.update(params[:user_id], :avatar_uri => params[:avatar])
      p("----user.avatar_uri.url after update: "+user.avatar_uri.url)


      if  user.avatar_uri

        parameters["success"]=true
        parameters["avatar_uri"]=user.avatar_uri.url
      else
        parameters["success"]=false
        parameters["error"]="Couldn't update user check the file used"
      end
    else
      parameters["success"]=false
      parameters["error"]="Couldn't find user"
    end
    render :json => parameters.to_json
  end

Here's the model user.rb

     class User < ApplicationRecord
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      mount_uploader :avatar_uri, AvatarUploader
      attr_accessor :login
      has_one :role, dependent: :destroy
      has_many :posts, dependent: :destroy
      has_many :likes, dependent: :destroy
      has_many :shares, dependent: :destroy
      has_many :comments, dependent: :destroy
      has_many :messages
      has_many :notifications, dependent: :destroy
      has_many :follows, class_name: "Follow", foreign_key: "follower_id", dependent: :destroy
      has_many :tokens
      has_and_belongs_to_many :conversations
    ...
end

and here's the uploader avatar_uploader.rb

class AvatarUploader < CarrierWave::Uploader::Base

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog
  def store_dir
    "#{model.class.to_s.underscore}/image"
  end

  def extension_whitelist
    %w(jpg jpeg gif png)
  end

  def cache_dir
    "tmp/"
  end

  def filename
    if model.is_used_fb_avatar
      original_filename
      #puts ("--------- is_used_fb_avatar is called ----")
    else
      SecureRandom.urlsafe_base64(16).to_s+ File.extname(original_filename).to_s if original_filename
      puts ("--------- is_used_fb_avatar is not called ----")
      puts ("filename => "+ SecureRandom.urlsafe_base64(16).to_s+File.extname(original_filename).to_s)
    end
  end


end

Here's the console log i get from the server :

Started PUT "/api/update-user-avatar.json" for 127.0.0.1 at 2017-06-30 21:10:43 +0200
Processing by Api::UsersController#update_avatar as JSON
  Parameters: {"user_id"=>"47", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f852d2c54e8 @tempfile=#<Tempfile:/var/folders/k7/2bnc1fxs1r164gf831dn5l140000gn/T/RackMultipart20170630-1261-1viplk8.jpg>, @original_filename="batman.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"batman.jpg\"\r\nContent-Type: image/jpeg\r\n">}
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 47], ["LIMIT", 1]]
"----user.avatar_uri.url before update: /user/image/wlOHg5N0K5aq5hJGFlM6XA.png"
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 47], ["LIMIT", 1]]
   (0.3ms)  BEGIN
  User Exists (0.5ms)  SELECT  1 AS one FROM "users" WHERE "users"."username" = $1 AND ("users"."id" != $2) LIMIT $3  [["username", "John"], ["id", 47], ["LIMIT", 1]]
--------- is_used_fb_avatar is not called ----
filename => jDZ48CtHI5a9L0qGcKdEWQ.jpg
  SQL (37.7ms)  UPDATE "users" SET "updated_at" = $1, "avatar_uri" = $2 WHERE "users"."id" = $3  [["updated_at", 2017-06-30 19:10:45 UTC], ["avatar_uri", "wlOHg5N0K5aq5hJGFlM6XA.png"], ["id", 47]]
--------- is_used_fb_avatar is not called ----
filename => njeFYobP6erVu9uYunmFhw.jpg
   (134.3ms)  COMMIT
"----user.avatar_uri.url after update: /user/image/wlOHg5N0K5aq5hJGFlM6XA.png"
Completed 200 OK in 683ms (Views: 136.7ms | ActiveRecord: 222.4ms)

You can see that def filename in the uploader gets called two times without saving model neither saving file ...

"wlOHg5N0K5aq5hJGFlM6XA.png" is an old value of the avatar_uri column and it doesnt get replaced by "njeFYobP6erVu9uYunmFhw.jpg" the new one ..

==>[ANSWER]: Solved it thanks to @Micael Nussbaumer ... the problem was using SecureRandom directly in my uploader so I changed it to this :

def filename
    if model.is_used_fb_avatar
      original_filename
      #puts ("--------- is_used_fb_avatar is called ----")
    else
      "avatar-#{secure_token}.jpg" if original_filename.present?
    end
  end

  protected

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end

Solution

  • Try this:

    def filename
      "avatar-#{secure_token}.jpg" if original_filename.present?
    end
    
    protected
    
    def secure_token
      var = :"@#{mounted_as}_secure_token"
      model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
    end
    

    Source for filenaming in carrierwave uploader