Ruby on rails 显示阵列中的单个图像时出现问题(Rails活动存储)
我相信这应该很简单,我忽略了一些愚蠢的事情 轨道6.1.1 Ruby 3.0.0 我正在使用活动存储将多张“照片”附加到我的文章模型 在文章“show.html.erb”中,我希望将照片放在文章中的多个不同位置,而不是在同一位置将所有照片循环成一行 show.html.erbRuby on rails 显示阵列中的单个图像时出现问题(Rails活动存储),ruby-on-rails,rails-activestorage,Ruby On Rails,Rails Activestorage,我相信这应该很简单,我忽略了一些愚蠢的事情 轨道6.1.1 Ruby 3.0.0 我正在使用活动存储将多张“照片”附加到我的文章模型 在文章“show.html.erb”中,我希望将照片放在文章中的多个不同位置,而不是在同一位置将所有照片循环成一行 show.html.erb <% @first2photos.each do |article| %> <%= image_tag(article.photos) %> <% end %> def
<% @first2photos.each do |article| %>
<%= image_tag(article.photos) %>
<% end %>
def show
@first2photos = Article.order("created_at DESC").first(2)
end
这会产生以下错误:
无法将图像解析为URL:未定义的方法“to#u model”用于#您的意思是?至yaml
如果我在“照片”上循环:
<% @first2photos.each do |photos| %>
<%= image_tag(@article.photos) %>
<% end %>
将其更改为:
<% @first2photos.each do |photos| %>
<%= image_tag(photos) %>
<% end %>
您需要在照片集合上循环,以便为每张照片使用
image\u标记
你的协会中有多张照片,你必须对这些照片进行迭代,然后对每张照片使用image\u标签
(不是所有照片都使用一个image\u标签
)谢谢,是不是“在多张图像上循环”?这就是在文章中循环(你有每个do | article
),你需要一个article.photos.each do | photo |
在第一个循环内谢谢,@arieljuod。还是不明白。更新了OP的更多细节。我添加了一个答案,让它更清晰
<% @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" = $1 LIMIT $2 [["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 $1 [["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)