2016-07-14 3 views
0

私のアプリケーションでeach_sliceを使用しての広告コードを4枚ごとに表示しています。Ruby on Rails - each_sliceを使用した場合の最終ラウンドを検索

これは私がそれをやっている方法です:私は最後のラウンドに広告を表示したくないので、私は最後のラウンドを見つけ、それを防ぐことができますどのように

- @post.images.each_slice(4) do |group| 
    - group.each do |image| 
    // My images 

    %div.ads 
    // My ads code 

広告を表示します。

私はそれがここで言っている行っている、しかし、運:How to know that we're on "the last round" of a .each_slice

が、私は、引数I私show -viewで実行/ before_actionそれとの私のpost_controllerと取得間違った番号にlast_slide方法を追加しません私はを私のコントローラにを実行しても何も起こりません。

+0

あなたはとても '場合image.id = images.lastのようなif文で囲むことができ.id' –

+0

ええ、 'group.include?(images.last)' – jphager2

+0

@AAnkudovichそれはうまくいきませんでした:/ – Rubioli

答えて

0

2通りの方法で行うことができます。第1のものは容易であり、第2の解決策はより複雑である。

1溶液(jphager2のおかげで):

if group.include?(images.last) 

2溶液:!

- @post.images.each_slice(2).each_with_index do |group, index| 
    - group.each_with_index do |image| 
    // your code 
    - if index < group.size 
    // Ad code 
0

4枚の画像の間に広告を挿入する目的を達成するために、collectionとspacer_templateオプションを指定してRailsのレンダリングメソッドを利用できます。

# The view 
= render partial: 'image_group', collection: @images.each_slice(4).to_a, spacer_template: 'advertisement' 

# _image_group.html.haml 
= render partial: 'image', collection: image_group 

# _image.html.haml 
// Your image code 

# _advertisement.html.haml 
%div.ads 
    // Your Ads Code 
関連する問題