简体   繁体   English

cycle()一个image_tag

[英]cycle() an image_tag

I'm want to display different image version: 我想显示不同的图像版本:

first article: big banner 第一篇文章:大旗帜

second: small banner that float to right/left 第二个:向右/向左浮动的小横幅

so, first thing: use cycle() but dont work: 所以,第一件事:使用cycle()但不工作:

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

or 要么

= image_tag(cycle(banner_big_path, banner_small_path))

Only first image is displayed 仅显示第一张图像

There's a proper way to make one like that ? 有一个合适的方法来制作这样的吗?

Your problem is that rails is expecting you to call cycle with the same set of strings each time. 你的问题是rails希望你每次都用同一组字符串调用循环。 At the moment you're passing a different pair of strings to each call to cycle, so rails resets the cycle each time. 目前,您正在为每个循环调用传递一组不同的字符串,因此每次都会重置循环。 New cycles always start with their first value, hence the result you describe. 新循环始终以其第一个值开始,因此您描述的结果。

Assuming your articles had methods called small_path , big_path , something like 假设你的文章有像small_pathbig_path这样的方法

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

Should return alternate image paths. 应返回备用图像路径。

You can make use of the session facility to store indexes there and use those. 您可以使用session工具在那里存储索引并使用它们。 For instance: 例如:

# 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

These helper methods approximate the interface you were asking for: 这些辅助方法接近您要求的接口:

 = image_tag(session_banner(banner_big, banner_small))

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

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