简体   繁体   English

Ruby on Rails - 每个 do 切片的不同元素

[英]Ruby on Rails - Different elements for each do slices

In my application I do each_slice and then each_with_index because of some stuff I'm doing in my application.在我的应用程序做each_slice然后each_with_index因为一些东西,我在我的应用程序正在做。

My current code is like this:我目前的代码是这样的:

- @images.each_slice(@album.images_per_page <= 2 ? 1 : 2) do |group|
  - group.each_with_index do |img, i|
    .col-md-2
      /* Ads Left */
    .col-md-7
      = image_tage img.image_url(:thumb)
    .col-md-3
      /* Ads Right */

Usually I show 4 images on every page.通常我在每一页上显示 4 张图片。 The first 3 will have ads inside .col-md-2 & .col-md-3 .前 3 个将在.col-md-2.col-md-3有广告。 What I want to achieve is to add different ads-codes/type for each element.我想要实现的是为每个元素添加不同的广告代码/类型。

For instance, First image would have 300x250 tag , second image 300x600 and third would have content-type ads (Taboola) and not showing any ads after these 3.例如,第一张图片会有300x250 标签,第二张图片300x600和第三张图片会有内容类型广告 (Taboola),并且在这 3 个之后不会展示任何广告。

I tried: (Ex. 1)我试过:(例1)

- if i == 0
  %h1 300x250
- elsif i == 1
  %h1 300x600
- elsif i == 2
  %h1 Taboola

and (Ex. 2)和(例 2)

- if i+1 == group.length
  %h1 i+1 = group.length

But I don't get what I'm looking for.但我没有得到我要找的东西。 Ex.1 takes every 1 element and adds 300x250 & every 2nd element and adds 300x600 , So I don't get Taboola and gets double of 300x250 & 300x600 while Ex.2 takes every 2nd element and adds only 300x250 . Ex.1 取每 1 个元素并添加300x250和每个第二个元素并添加300x600 ,所以我没有得到Taboola并获得300x250300x600 的两倍,而 Ex.2 取每个第二个元素并仅添加300x250

Ex.1 results becomes this: Ex.1 结果变成这样:

Image 1 => 300x250
Image 2 => 300x600
Image 3 => 300x250
Image 4 => 300x600

Ex.2 results becomes this: Ex.2 结果变成这样:

Image 1 => /* Nothing */
Image 2 => 300x250
Image 3 => /* Nothing */
Image 4 => 300x250

How can I achieve so I can choose what ad I want to show on each element?我该如何实现才能选择要在每个元素上展示的广告?

Result I'm looking for:我正在寻找的结果:

Image 1 => 300x250
Image 2 => 300x600
Image 3 => Taboola
Image 4 => /* Nothing */

Hope I could explain what I'm looking for.希望我能解释我在找什么。

The goal should be to leave as few logic as possible in views.目标应该是在视图中留下尽可能少的逻辑。 Complex logic should be done in model/controller/helpers.复杂的逻辑应该在模型/控制器/助手中完成。

Here's a possibility with zip :这是zip的一种可能性:

@images = %w(imageA imageB imageC imageD)

params = [
  ['300x250','r1'],
  ['300x600','r2'],
  ['Taboola','r3']
]

@images.zip(params).each_with_index do |(image, param), i|
  left, right = param
  puts "Image : #{image}"
  puts "ParamL: #{left}"
  puts "ParamR: #{right}"
  puts "Index : #{i}"
  puts "Even? : #{i.even?}"
  puts
end

It outputs :它输出:

Image : imageA
ParamL: 300x250
ParamR: r1
Index : 0
Even? : true

Image : imageB
ParamL: 300x600
ParamR: r2
Index : 1
Even? : false

Image : imageC
ParamL: Taboola
ParamR: r3
Index : 2
Even? : true

Image : imageD
ParamL: 
ParamR: 
Index : 3
Even? : false

It seems to be enough information for what you want to do.对于您想要做的事情,这似乎是足够的信息。

Also, you're calling each_slice with either 1 or 2 as parameter.此外,您使用 1 或 2 作为参数调用each_slice each_slice(1) is basically useless, each_slice(2) would just split your array in pairs. each_slice(1)基本上没用, each_slice(2)只会将你的数组分成两对。 Checking if id is odd or even should give you just as much information, without needing another nested block.检查id是奇数还是偶数应该为您提供尽可能多的信息,而无需另一个嵌套块。

each_slice(2) is also the reason why your index stays equal to 0 or 1 . each_slice(2)也是您的索引保持等于01 It cannot be equal to 2, so your Taboola case never happened.它不能等于 2,因此您的Taboola案例从未发生过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM