无法显示阵列中的单个图像(Rails 活动存储)
Trouble displaying individual images from array (Rails active storage)
我相信这应该很简单,但我忽略了一些愚蠢的事情。
Rails6.1.1
Ruby3.0.0
我正在使用活动存储将多个 'photos' 附加到我的文章模型。
在文章中 'show.html.erb' 我想将照片放在文章中的多个不同位置,而不是在同一位置连续循环所有照片。
show.html.erb
<% @first2photos.each do |article| %>
<%= image_tag(article.photos) %>
<% end %>
articles.controller.erb
def show
@first2photos = Article.order("created_at DESC").first(2)
end
这给出了以下错误:
Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::Attached::Many:0x00007fdbbfa852a8 @name="photos", @record=#<Article id: 1, title: "This is the first post", snippet: "Hopefully this will also attach a couple of photos", body: "Nullam ac odio eget mauris accumsan malesuada. Nul...", created_at: "2021-01-30 20:50:31.766713000 +0000", updated_at: "2021-01-30 21:04:09.424224000 +0000">> Did you mean? to_yaml
如果我循环使用“照片”:
<% @first2photos.each do |photos| %>
<%= image_tag(@article.photos) %>
<% end %>
我仍然收到以下错误:
Can't resolve image into URL: undefined method `to_model'...
将其更改为:
<% @first2photos.each do |photos| %>
<%= image_tag(photos) %>
<% end %>
页面现已加载,但显示损坏的图像图标,没有错误。检查终端中的日志,我不确定我是否可以看到它试图访问照片的位置。
Started GET "/articles/1" for ::1 at 2021-01-31 20:56:58 +0000
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"1"}
Article Load (2.9ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
↳ app/controllers/articles_controller.rb:64:in `set_article'
Article Load (1.4ms) SELECT "articles".* FROM "articles" ORDER BY created_at DESC LIMIT [["LIMIT", 2]]
↳ app/controllers/articles_controller.rb:11:in `show'
Rendering layout layouts/application.html.erb
Rendering articles/show.html.erb within layouts/application
Rendered articles/show.html.erb within layouts/application (Duration: 0.4ms | Allocations: 155)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 8.6ms | Allocations: 4208)
Completed 200 OK in 20ms (Views: 9.3ms | ActiveRecord: 4.4ms | Allocations: 6022)
您需要遍历照片集才能对每张照片使用 image_tag
。
<% @first2photos.each do |article| %>
<% article.photos.each do |photo|
<%= image_tag(photo) %>
<% end %>
<% end %>
如果您只想在每篇文章中显示一张图片,则:
<% @first2photos.each do |article| %>
<%= image_tag(article.photos.first) %>
<% end %>
我会将名称 first2photos
更改为 first2articles
以减少混淆。
我相信这应该很简单,但我忽略了一些愚蠢的事情。
Rails6.1.1 Ruby3.0.0
我正在使用活动存储将多个 'photos' 附加到我的文章模型。
在文章中 'show.html.erb' 我想将照片放在文章中的多个不同位置,而不是在同一位置连续循环所有照片。
show.html.erb
<% @first2photos.each do |article| %>
<%= image_tag(article.photos) %>
<% end %>
articles.controller.erb
def show
@first2photos = Article.order("created_at DESC").first(2)
end
这给出了以下错误:
Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::Attached::Many:0x00007fdbbfa852a8 @name="photos", @record=#<Article id: 1, title: "This is the first post", snippet: "Hopefully this will also attach a couple of photos", body: "Nullam ac odio eget mauris accumsan malesuada. Nul...", created_at: "2021-01-30 20:50:31.766713000 +0000", updated_at: "2021-01-30 21:04:09.424224000 +0000">> Did you mean? to_yaml
如果我循环使用“照片”:
<% @first2photos.each do |photos| %>
<%= image_tag(@article.photos) %>
<% end %>
我仍然收到以下错误:
Can't resolve image into URL: undefined method `to_model'...
将其更改为:
<% @first2photos.each do |photos| %>
<%= image_tag(photos) %>
<% end %>
页面现已加载,但显示损坏的图像图标,没有错误。检查终端中的日志,我不确定我是否可以看到它试图访问照片的位置。
Started GET "/articles/1" for ::1 at 2021-01-31 20:56:58 +0000
Processing by ArticlesController#show as HTML
Parameters: {"id"=>"1"}
Article Load (2.9ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = LIMIT [["id", 1], ["LIMIT", 1]]
↳ app/controllers/articles_controller.rb:64:in `set_article'
Article Load (1.4ms) SELECT "articles".* FROM "articles" ORDER BY created_at DESC LIMIT [["LIMIT", 2]]
↳ app/controllers/articles_controller.rb:11:in `show'
Rendering layout layouts/application.html.erb
Rendering articles/show.html.erb within layouts/application
Rendered articles/show.html.erb within layouts/application (Duration: 0.4ms | Allocations: 155)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 8.6ms | Allocations: 4208)
Completed 200 OK in 20ms (Views: 9.3ms | ActiveRecord: 4.4ms | Allocations: 6022)
您需要遍历照片集才能对每张照片使用 image_tag
。
<% @first2photos.each do |article| %>
<% article.photos.each do |photo|
<%= image_tag(photo) %>
<% end %>
<% end %>
如果您只想在每篇文章中显示一张图片,则:
<% @first2photos.each do |article| %>
<%= image_tag(article.photos.first) %>
<% end %>
我会将名称 first2photos
更改为 first2articles
以减少混淆。