ruby-on-rails - 使用 Active Storage 部分工作的简单图像上传
问题描述
任何帮助或指导将不胜感激。
但是,我遵循了 Rails 指南,并且可以将图像上传到模型。但是,它会产生错误。
我已经复制了错误提示的内容,这会产生另一个错误并且不上传图像。
非常非常感谢提前!
这可行,但会在表单提交后产生错误。如果我回击然后访问特定的产品 ID 产品#show 图片已上传。
错误
ProductsController 中的 NameError #create undefined local variable or method `product' for # 你的意思是?@产品
产品.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 。
产品.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
Products#show 中的 ArgumentError 显示 /Users/user/rubyonrails/shopping/app/views/products/show.html.erb 其中第 2 行出现:
无法将图像解析为 URL:to_model 委托给附件,但附件为 nil
第二个终端输出显示上传似乎可以工作,然后称为 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
项目清单
解决方案
鸡蛋在我脸上。
感叹号省略似乎是问题所在。
从
@product = @list.products.create(product_params)
对此,它有效
@product = @list.products.create!(product_params)
推荐阅读
- chart.js - ChartJS timeline graph with events
- vue.js - 如何更新以下代码中的 msg 变量?
- node.js - 如何在 Elastic Beanstalk 上使用 Django 提供 React 应用程序?
- flutter - Flutter中的异步函数不返回null
- pycharm - 在 Windows 10 上使用 Pycharm 配置 Odoo
- sitecore8.2 - 在哪里下载 Sitecore.support.98798 补丁
- javascript - 如何使用 react-compound-timer getTime() 方法更新组件状态
- python - 在 python 中安装和导入带有 rasa-nlu 的 Matplotlib 库时出错
- javascript - 使用更新面板进行网格视图图像缩放的问题
- python - python中列表和函数的问题