首页 > 解决方案 > how to access specific ActiveStorage image in multiple images

问题描述

I am using ActiveStorage for Ruby on Rails project. A post has many images and I want to use the one of images with headline. I am wondering how to access specific image in multiple images.

Current code is like this. I create hash(key: post ID, value: image) and access hash data in view. I can show the image but is there another better way to access specific image?

@posts.each do |p|
  if p.post_images.present? && p.headline_image_id.present?
    @headline_image_id.store(p.id, p.post_images.select {|image| image.blob_id == p.headline_image_id})
  end
end

标签: ruby-on-railsrails-activestorage

解决方案


推荐阅读