简体   繁体   中英

Rails app Carrierwave uploads work on development but stop working on production

My uploads work perfectly on my local machine, but fail to work on production server. Code is identical, there are no unsubmitted commits etc.

Here is what happens on my local machine:

Started POST "/admin/projects" for 127.0.0.1 at 2015-10-08 17:17:11 +0300
Processing by Admin::ProjectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"BLD9208mLcwkXDjyqYbf7sFBs51mgrGAGZYFIi2VT+jstdj0FdC0PB+DmrX3epz7m7UU5LXmN+961/9EN7ExQA==", "projectphotos"=>[#<ActionDispatch::Http::UploadedFile:0x007fbffb8133d8 @tempfile=#<Tempfile:/var/folders/dz/tf2gsysx18n_cj_m1xqbpyw80000gn/T/RackMultipart20151008-24531-w7z0b4.jpg>, @original_filename="_MG_9839.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9839.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fbffb813360 @tempfile=#<Tempfile:/var/folders/dz/tf2gsysx18n_cj_m1xqbpyw80000gn/T/RackMultipart20151008-24531-1f7lkwr.jpg>, @original_filename="_MG_9847.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9847.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fbffb8132e8 @tempfile=#<Tempfile:/var/folders/dz/tf2gsysx18n_cj_m1xqbpyw80000gn/T/RackMultipart20151008-24531-1u4h5f2.jpg>, @original_filename="_MG_9857.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9857.jpg\"\r\nContent-Type: image/jpeg\r\n">], "project"=>{"title"=>"Тестик", "desctitle"=>"Проектик", "description"=>"пупик", "teammember_ids"=>["6", ""]}, "commit"=>"Сохранить проект"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  Teammember Load (0.2ms)  SELECT  "teammembers".* FROM "teammembers" WHERE "teammembers"."id" = $1 LIMIT 1  [["id", 6]]
   (0.1ms)  BEGIN
  SQL (1.8ms)  INSERT INTO "projects" ("title", "description", "desctitle", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["title", "Тестик"], ["description", "пупик"], ["desctitle", "Проектик"], ["created_at", "2015-10-08 14:17:11.018140"], ["updated_at", "2015-10-08 14:17:11.018140"]]
  SQL (1.4ms)  INSERT INTO "participations" ("teammember_id", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["teammember_id", 6], ["project_id", 51], ["created_at", "2015-10-08 14:17:11.021601"], ["updated_at", "2015-10-08 14:17:11.021601"]]
   (1.4ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (3.4ms)  INSERT INTO "projectphotos" ("image", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["image", "_MG_9839.jpg"], ["project_id", 51], ["created_at", "2015-10-08 14:17:12.074996"], ["updated_at", "2015-10-08 14:17:12.074996"]]
   (0.5ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "projectphotos" ("image", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["image", "_MG_9847.jpg"], ["project_id", 51], ["created_at", "2015-10-08 14:17:12.914800"], ["updated_at", "2015-10-08 14:17:12.914800"]]
   (0.4ms)  COMMIT
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "projectphotos" ("image", "project_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["image", "_MG_9857.jpg"], ["project_id", 51], ["created_at", "2015-10-08 14:17:13.759695"], ["updated_at", "2015-10-08 14:17:13.759695"]]
   (0.4ms)  COMMIT
Redirected to http://localhost:9292/admin/projects
Completed 302 Found in 2780ms (ActiveRecord: 11.4ms)

Just perfect, images being uploaded and so on.

Now, the exact same operation on production server:

I, [2015-10-08T10:23:50.330774 #13367]  INFO -- : Started POST "/admin/projects" for 89.169.175.47 at 2015-10-08 10:23:50 -0400
I, [2015-10-08T10:23:50.334229 #13367]  INFO -- : Processing by Admin::ProjectsController#create as HTML
I, [2015-10-08T10:23:50.334437 #13367]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"2hx8xJdCrvkmwkfKGI4Y/gzu3bc/hPWNsrWgtNo/97kWFWKauYOrWh6v15mHQ/28sqaaAb08hEhHrkuvFjmxPw==", "projectphotos"=>[#<ActionDispatch::Http::UploadedFile:0x007f6d9006fc98 @tempfile=#<Tempfile:/tmp/RackMultipart20151008-13367-18ri23t.jpg>, @original_filename="_MG_9839.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9839.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f6d9006fc20 @tempfile=#<Tempfile:/tmp/RackMultipart20151008-13367-yrlhkm.jpg>, @original_filename="_MG_9847.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9847.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f6d9006fba8 @tempfile=#<Tempfile:/tmp/RackMultipart20151008-13367-1l2x4tu.jpg>, @original_filename="_MG_9857.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"_MG_9857.jpg\"\r\nContent-Type: image/jpeg\r\n">], "project"=>{"title"=>"Тестовый проект", "desctitle"=>"Тестик", "description"=>"Тестовый проект", "teammember_ids"=>[""]}, "commit"=>"Сохранить проект"}
D, [2015-10-08T10:23:50.337997 #13367] DEBUG -- :   User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
D, [2015-10-08T10:23:50.343076 #13367] DEBUG -- :    (0.1ms)  BEGIN
D, [2015-10-08T10:23:50.346752 #13367] DEBUG -- :   SQL (1.2ms)  INSERT INTO "projects" ("title", "description", "desctitle", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["title", "Тестовый проект"], ["description", "Тестовый проект"], ["desctitle", "Тестик"], ["created_at", "2015-10-08 14:23:50.343540"], ["updated_at", "2015-10-08 14:23:50.343540"]]
D, [2015-10-08T10:23:50.348048 #13367] DEBUG -- :    (0.7ms)  COMMIT
D, [2015-10-08T10:23:50.349034 #13367] DEBUG -- :    (0.2ms)  BEGIN
D, [2015-10-08T10:23:50.373831 #13367] DEBUG -- :    (0.3ms)  COMMIT
D, [2015-10-08T10:23:50.374391 #13367] DEBUG -- :    (0.2ms)  BEGIN
D, [2015-10-08T10:23:50.384639 #13367] DEBUG -- :    (0.2ms)  COMMIT
D, [2015-10-08T10:23:50.385026 #13367] DEBUG -- :    (0.2ms)  BEGIN
D, [2015-10-08T10:23:50.392234 #13367] DEBUG -- :    (0.2ms)  COMMIT
I, [2015-10-08T10:23:50.393967 #13367]  INFO -- : Redirected to http://46.101.161.208/admin/projects
I, [2015-10-08T10:23:50.394322 #13367]  INFO -- : Completed 302 Found in 60ms (ActiveRecord: 6.2ms)

Images are in the params hash, but are not inserted into database. Could someone hint me into the right direction?

Here is my projects_controller.rb:

def update
        if @project.update(project_params)
            if params[:projectphotos]

                params[:projectphotos].each { |image| @project.projectphotos.create(image: image) }
            end
            redirect_to admin_projects_path
            flash[:success] = "Updated"
        end
    end

    def edit
    end

    def show
    end

    def create
        @project = Project.new(project_params)
        if @project.save
            if params[:projectphotos]
                params[:projectphotos].each { |image| @project.projectphotos.create(image: image) }
            end
            redirect_to admin_projects_path
            flash[:success] = 'Created'
        else
            flash[:alert] = 'Something went wrong'
            render 'new'
        end
    end

This exact same code already worked on production some time afo, but I think at some point, after I added ImageMagick for resizing it stopped working. I switched to MiniMagick and while on other projects resizing and image uploading works well with MiniMagick, here I have this issue on production. To be honest, this is why I don't believe, that the issue is caused by MiniMagick, just wanted to share this info.

Update: As requested, here is my uploader code:

# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end



  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end
  version :thumb_next do
    process :resize_to_fill => [600, 460]
  end

  version :main_image do
    process :resize_to_fill => [1170, 700]
  end

  version :thumb_small do
    process :resize_to_fit => [50,50]
  end

  version :thumb_medium do
    process :resize_to_fit => [350,350]
  end

end

Update 2: This is getting even more ridiculous, I added some simple outputs to debug my production code:

projects_controller.rb

def update
        if @project.update(project_params)
            if params[:projectphotos]
                p "inside params"
                params[:projectphotos].each do |image| 
                    p image
                    p "inside of each image param"
                    if @project.projectphotos.create(image: image) 
                        p "created image"
                    end
                end
            end
            redirect_to admin_projects_path
            flash[:success] = "Updated"
        end
    end

With that update I get this on production:

deploy@rails:~$ cat apps/furnitureapp/current/log/puma.error.log 
=== puma startup: 2015-10-12 05:23:35 -0400 ===
* Inherited unix:///home/deploy/apps/furnitureapp/shared/tmp/sockets/furnitureapp-puma.sock
"inside params"
#<ActionDispatch::Http::UploadedFile:0x007f6e08304828 @tempfile=#<Tempfile:/tmp/RackMultipart20151012-26945-1wzj8kl.jpg>, @original_filename="American-Psycho-blood.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"American-Psycho-blood.jpg\"\r\nContent-Type: image/jpeg\r\n">
"inside of each image param"
"created image"
#<ActionDispatch::Http::UploadedFile:0x007f6e083047b0 @tempfile=#<Tempfile:/tmp/RackMultipart20151012-26945-ahjgw2.jpg>, @original_filename="i-love-sales.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"projectphotos[]\"; filename=\"i-love-sales.jpg\"\r\nContent-Type: image/jpeg\r\n">
"inside of each image param"
"created image"

But still no images and still no INSERT sql-queries! Projectphoto.all shows no entries, although it clearly should have created an image.

Ok, apparently there was no imagemagick installed on my production server.

Thanks to this article - https://chuanhesmile.wordpress.com/2014/12/13/issue-fixed-rollback-transaction-when-uploading-images-using-carrierwave/

The problem I had - there was no error message, nor a rollback when performing a 'create' action. Instead I rewrote my controller into using 'build' and 'save'.

def update
        if @project.update(project_params)
            if params[:projectphotos]
                p "inside params"
                params[:projectphotos].each do |image| 
                    p image
                    p "inside of each image param"
                    @newImage = @project.projectphotos.build(image: image) 
                    if @newImage.save
                        p "saved image"
                    end
                end
            end
            redirect_to admin_projects_path
            flash[:success] = "Обновлено"
        end
    end

This gave me at least a rollback.

I was able to debug the rollback thanks to this SO question how to find the cause of ActiveRecord ROLLBACK (important to save your file with a BANG - '!'). This finally led to some understandable errors:

2.2.0 :011 > @pr.save!
   (1.0ms)  BEGIN
   (0.4ms)  ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Projectphotos image Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: ImageMagick/GraphicsMagick is not installed

Maybe it helps someone.

请检查是否已安装ImageMagick / GraphicsMagick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM