开发者

How to avoid duplicate entries in nested iteration

How can I rewrite this to avoid duplicate entries?

images.each do |img|
  thumbs.each do |th|
    html << link_to(image_tag("#{th.url}"), "#{img.url}")
  end
end

I want to wrap thumbnail images开发者_StackOverflow中文版 th.url into links to original images img.url

up:

I'm using a fog gem to get images and thumbs from S3.

They're files with different prefixes: storage.directories.get(bucket, :prefix => "thumbs").files


Why not relate your images and thumbnails in some way?

So if your image is called image_name.jpg you could have your thumbnail called thumbs/image_name.jpg.

If your names are unconnected, then why not just associate them in your application so you use an associative array of images and thumbnail names?

my_images = [ "image_1.jpg"=>"aflafffff_thumb.jpg", "image_2.jpg"->"zofofroro_thumb.jpg" ] 

Either of those ways enable you to just find the corresponding thumbnail for each image.


You're looping through two collections (images and thumbs) - hence the duplicates. Guessing your image and thumb objects are linked somehow...

e.g. thumbs available doing something like image.thumb

images.each do |image|
  html << link_to(image_tag(image.thumb.url), image.url)
end

By only iterating through the images collection you won't get duplicates.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜