简体   繁体   English

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

[英]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. 图像位于params哈希中,但未插入数据库中。 Could someone hint me into the right direction? 有人可以提示我正确的方向吗?

Here is my projects_controller.rb: 这是我的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. 完全相同的代码已经可以在生产中工作了一段时间,但是我认为在添加ImageMagick调整大小后,它已经停止工作了。 I switched to MiniMagick and while on other projects resizing and image uploading works well with MiniMagick, here I have this issue on production. 我改用MiniMagick,而在其他项目中,使用MiniMagick调整大小和上传图片效果很好,但是我在生产中遇到了这个问题。 To be honest, this is why I don't believe, that the issue is caused by MiniMagick, just wanted to share this info. 老实说,这就是为什么我不相信这个问题是由MiniMagick引起的,只想分享此信息。

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: 更新2:这变得更加荒谬,我添加了一些简单的输出来调试生产代码:

projects_controller.rb 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! 但是仍然没有图像,也没有插入sql查询! Projectphoto.all shows no entries, although it clearly should have created an image. Projectphoto.all没有显示任何条目,尽管它显然应该已经创建了图像。

Ok, apparently there was no imagemagick installed on my production server. 好的,显然我的生产服务器上没有安装imagemagick。

Thanks to this article - https://chuanhesmile.wordpress.com/2014/12/13/issue-fixed-rollback-transaction-when-uploading-images-using-carrierwave/ 感谢这篇文章-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'. 相反,我改写了控制器,使其使用“ 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

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 - '!'). 由于有这样的问题,我能够调试回滚(Rollback)( 如何找到ActiveRecord ROLLBACK的原因) (重要的是使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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