簡體   English   中英

cycle()一個image_tag

[英]cycle() an image_tag

我想顯示不同的圖像版本:

第一篇文章:大旗幟

第二個:向右/向左浮動的小橫幅

所以,第一件事:使用cycle()但不工作:

= cycle(image_tag(banner_big), image_tag(banner_small)

要么

= image_tag(cycle(banner_big_path, banner_small_path))

僅顯示第一張圖像

有一個合適的方法來制作這樣的嗎?

你的問題是rails希望你每次都用同一組字符串調用循環。 目前,您正在為每個循環調用傳遞一組不同的字符串,因此每次都會重置循環。 新循環始終以其第一個值開始,因此您描述的結果。

假設你的文章有像small_pathbig_path這樣的方法

article.send(cycle("big_path","small_path"))

應返回備用圖像路徑。

您可以使用session工具在那里存儲索引並使用它們。 例如:

# application_helper.rb
def session_banner_index
  session[:banner_index] || 0
end

def session_banner(*list)
  list[session_banner_index % list.length]
end

# application_controller.rb
def increment_session_banner_index!
  session[:banner_index] = (session[:banner_index] || 0) + 1
end

這些輔助方法接近您要求的接口:

 = image_tag(session_banner(banner_big, banner_small))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM