简体   繁体   English

使用 Active Storage 部分工作的简单图像上传

[英]Partially working simple image upload using Active Storage

Any help or guidance would be most appreciated.任何帮助或指导将不胜感激。

I've followed the Rails Guide, however, and I can upload an image to a model.但是,我遵循了 Rails 指南,可以将图像上传到 model。 However, it produces an error.但是,它会产生错误。

I've copied what the error suggests and that produces another error and does not upload an image.我已经复制了错误提示的内容,这会产生另一个错误并且不上传图像。

Many, many thanks in advance!非常非常感谢提前!

This works but produces an error post form submission.这可行,但会在表单提交后产生错误。 If I hit back and then visit the specific prod id product#show the image has uploaded.如果我回击然后访问特定的产品 ID 产品#show 图片已上传。

Error错误

NameError in ProductsController#create undefined local variable or method `product' for # Did you mean? ProductsController 中的 NameError #create undefined local variable or method `product' for # 你的意思是? @product @产品

Products.rb产品.rb

    def create
        @list = List.find(params[:list_id])
        @product = @list.products.create(product_params)
        product.hero.attach(params[:hero])
        redirect_to list_path(@list)
    end

This does not work however, looks syntactically correct as the "@product" model attaches to the:hero.但是,这不起作用,看起来语法正确,因为“@product”model 附加到:hero。

Products.rb产品.rb

    def create
        @list = List.find(params[:list_id])
        @product = @list.products.create(product_params)
        @product.hero.attach(params[:hero])
        redirect_to list_path(@list)
    end
  • creates the product创建产品
  • refreshes the screen back to the product list so it looks correct.将屏幕刷新回产品列表,使其看起来正确。
  • This breaks producing two errors:这会产生两个错误:

The first error on products#show产品上的第一个错误#show

ArgumentError in Products#show Showing /Users/user/rubyonrails/shopping/app/views/products/show.html.erb where line #2 raised: Products#show 中的 ArgumentError 显示 /Users/user/rubyonrails/shopping/app/views/products/show.html.erb 其中第 2 行出现:

Can't resolve image into URL: to_model delegated to attachment, but the attachment is nil无法将图像解析为 URL:to_model 委托给附件,但附件为零

Second terminal output presents the upload seems to work then something called Active Storage Purge fires up removing it?第二个终端 output 显示上传似乎可以工作,然后称为 Active Storage Purge 的东西会启动删除它?

  ActiveStorage::Blob Load (0.3ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] Performing ActiveStorage::PurgeJob (Job ID: e9b35a62-ff6c-4a38-8946-3aa9c19668ef) from Async(active_storage_purge) enqueued at 2020-04-15T11:31:41Z with arguments: #<GlobalID:0x00007f92b1da3d60 @uri=#<URI::GID gid://shopping/ActiveStorage::Blob/7>>
[ActiveJob] [ActiveStorage::AnalyzeJob] [b724849f-6993-4130-bef1-a0f8837a3171]    (6.4ms)  COMMIT
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef]    (0.8ms)  BEGIN
[ActiveJob] [ActiveStorage::AnalyzeJob] [b724849f-6993-4130-bef1-a0f8837a3171] Performed ActiveStorage::AnalyzeJob (Job ID: b724849f-6993-4130-bef1-a0f8837a3171) from Async(active_storage_analysis) in 15.88ms
Started GET "/lists/9" for ::1 at 2020-04-15 12:31:41 +0100
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef]   ActiveStorage::Attachment Exists? (0.5ms)  SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."blob_id" = $1 LIMIT $2  [["blob_id", 7], ["LIMIT", 1]]
Processing by ListsController#show as HTML
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef]   ActiveStorage::Attachment Load (1.1ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 7], ["record_type", "ActiveStorage::Blob"], ["name", "preview_image"], ["LIMIT", 1]]
  Parameters: {"id"=>"9"}
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef]   ActiveStorage::Blob Destroy (1.0ms)  DELETE FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1  [["id", 7]]
  List Load (0.9ms)  SELECT "lists".* FROM "lists" WHERE "lists"."id" = $1 LIMIT $2  [["id", 9], ["LIMIT", 1]]
  ↳ app/controllers/lists_controller.rb:7:in `show'
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 9], ["LIMIT", 1]]
  ↳ app/controllers/lists_controller.rb:8:in `show'
  Rendering lists/show.html.erb within layouts/application
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/lists/show.html.erb:2
  Product Exists? (0.2ms)  SELECT 1 AS one FROM "products" WHERE "products"."list_id" = $1 LIMIT $2  [["list_id", 9], ["LIMIT", 1]]
  ↳ app/views/lists/show.html.erb:5
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."list_id" = $1  [["list_id", 9]]
  ↳ app/views/lists/show.html.erb:6
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef]    (6.4ms)  COMMIT
   (0.8ms)  SELECT COUNT(*) FROM "products" WHERE "products"."list_id" = $1  [["list_id", 9]]
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef]   Disk Storage (0.2ms) Deleted file from key: 31cfuow9pj6vjqhq8i479fdxf1lc
  ↳ app/controllers/application_controller.rb:5:in `product_list_size?'
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef]   Disk Storage (0.1ms) Deleted files by key prefix: variants/31cfuow9pj6vjqhq8i479fdxf1lc/
  Rendered lists/show.html.erb within layouts/application (Duration: 9.5ms | Allocations: 7278)
[ActiveJob] [ActiveStorage::PurgeJob] [e9b35a62-ff6c-4a38-8946-3aa9c19668ef] Performed ActiveStorage::PurgeJob (Job ID: e9b35a62-ff6c-4a38-8946-3aa9c19668ef) from Async(active_storage_purge) in 22.5ms
[Webpacker] Everything's up-to-date. Nothing to do

List item项目清单

Egg on my face.鸡蛋在我脸上。

The exclamation mark omission seems to be the issue.感叹号省略似乎是问题所在。

from

@product = @list.products.create(product_params)

to this and it works对此,它有效

@product = @list.products.create!(product_params)

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

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