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

问题描述 投票:0回答:1

任何帮助或指导,将不胜感激。

但是,我遵循了Rails指南,并且可以将图像上传到模型。但是,它会产生一个错误。

我已经复制了错误提示,这又产生了另一个错误,并且没有上传图像。

非常感谢,谢谢!

这有效,但提交表单后产生错误。如果我回击然后访问特定的产品ID产品#,则表明图像已上传。

错误

ProductController中的NameError #create#的未定义局部变量或方法`product'的意思? @product

Products.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

但是,这在语法上是正确的,因为“ @product”模型附加在:hero上。

Products.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
  • 创建产品
  • 将屏幕刷新回到产品列表,因此看起来正确。
  • 此中断会产生两个错误:

产品上的第一个错误#show

产品中的ArgumentError #show显示/Users/user/rubyonrails/shopping/app/views/products/show.html.erb,其中第2行出现:

无法将图像解析为URL:委托给附件的to_model,但附件为零

第二个终端输出显示上载似乎有效,然后启动了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

列表项

ruby-on-rails
1个回答
0
投票

鸡蛋在我的脸上。

似乎缺少感叹号。

来自

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

为此,它起作用

@product = @list.products.create!(product_params)
© www.soinside.com 2019 - 2024. All rights reserved.