0
只是想了解如何將嵌套圖像重新顯示在我的首頁上。我對標準模型沒有問題,但一直無法找到如何通過has_many嵌套屬性。我所有的嵌套窗體工作正常,只是忽略了前端。Rails 4檢索嵌套屬性以便在index.html.erb中顯示
例如。產品嵌套了product_images。這看起來並不是一個聰明的做法,因爲上傳的最後五張圖片並不一定與最後添加的五款產品相關。
有人可以分享一個例子。
歡呼
應用程序/控制器/ home_controller.rb
class HomeController < ApplicationController
def index
@products = Product.last(5)
@product_images = ProductImage.last(5)
end
end
應用程序/視圖/家庭/ index.html.erb
<% @products.each do |pd| %>
<div><%= pd.product_name %>
<% end %>
<% @product_images.each do |pd| %>
<%= image_tag (pd.product_image(:medium)) %>
<% end %>
</div>
甜pluk沒有工作,但將其改爲.MAP'高清指數 @products = Product.last(5) @product_ids = @ products.map(&:id) @product_images = ProductImage.where(:product_id => @product_ids).last(5) end' –
您也可以使用'collect' – webster
他們之間有很大的區別嗎? –