Rails应用程序Carrierwave上传工作在开发上,但停止生产

时间:2015-10-08 14:38:15

标签: ruby-on-rails carrierwave minimagick

我的上传功能完全适用于我的本地计算机,但无法在生产服务器上运行。代码相同,没有未提交的提交等。

以下是本地计算机上发生的情况:

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)

完美,图片上传等等。

现在,生产服务器上完全相同的操作:

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)

图像在params散列中,但未插入数据库。有人会暗示我走向正确的方向吗?

这是我的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

这个完全相同的代码已经在生产的某个时间用于生产,但我想在某些时候,在我添加ImageMagick进行调整后它停止工作。我转而使用MiniMagick,而在其他项目中,调整大小和图像上传与MiniMagick配合得很好,这里我有关于制作的这个问题。说实话,这就是为什么我不相信,这个问题是由MiniMagick引起的,只是想分享这些信息。

更新: 根据要求,这是我的上传代码:

# 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

更新2: 这变得更加荒谬,我添加了一些简单的输出来调试我的生产代码:

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

有了这个更新,我就会在生产中得到这个:

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"

但仍然没有图像,仍然没有INSERT sql-queries! Projectphoto.all显示没有条目,虽然它显然应该创建了一个图像。

2 个答案:

答案 0 :(得分:0)

好的,显然我的生产服务器上没有安装imagemagick。

感谢这篇文章 - https://chuanhesmile.wordpress.com/2014/12/13/issue-fixed-rollback-transaction-when-uploading-images-using-carrierwave/

我遇到的问题 - 执行“创建”操作时没有错误消息,也没有回滚。相反,我重写了我的控制器使用'build'和'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

这给了我至少一次回滚。

由于这个SO问题how to find the cause of ActiveRecord ROLLBACK,我能够调试回滚(重要的是用BANG保存文件 - '!')。这最终导致了一些可以理解的错误:

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

也许对某人有所帮助。

答案 1 :(得分:-1)

请检查是否已安装ImageMagick / GraphicsMagick