[英]How do I show two different partials on every other pageview?
I am using the cycle
Rails method - largely because I couldn't think of another way. 我正在使用
cycle
Rails方法-主要是因为我想不出另一种方法。
All I am doing, is in my controller declaring this: 我正在做的是在控制器中声明以下内容:
@banner_cycle = [1,2]
And in my view, doing this: 在我看来,这样做是:
<div class="hidden-sm hidden-xs text-center">
<% @banner_cycle.each do |x| %>
<%= cycle(render "shared/banner_728x90", render "shared/banner_970x250") %>
<% end %>
</div>
This is the error I am getting though: 这是我得到的错误:
SyntaxError at /posts/another-awesome-test
syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
Unless it's changed or I've forgotten cycle
doesn't work across page views. 除非更改了它,否则我忘记了
cycle
不会在页面浏览中起作用。 It only works for the current view/page-load. 它仅适用于当前视图/页面加载。 If you really want to alternate page views you'll have to keep state somewhere on the server.
如果您确实要替换页面视图,则必须将状态保持在服务器上的某个位置。 Or, if you're okay alternating per user you can track it in a session.
或者,如果您可以按用户交替显示,则可以在会话中进行跟踪。 Say something like...
说出类似...
In your controller action: 在控制器操作中:
session[:banner_cycle] = session[:banner_cycle].to_i + 1
The .to_i
is to protect against nil. .to_i
是为了防止nil。
In your view: 您认为:
<div class="hidden-sm hidden-xs text-center">
<% if session[:banner_cycle].even? %>
<%= render "shared/banner_728x90" %>
<% else %>
<%= render "shared/banner_970x250" %>
<% end %>
</div>
I suppose an aggressive user could overflow the maximum integer value, but I'm not going to worry about that here. 我想一个激进的用户可能会溢出最大整数值,但是我在这里不必担心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.