简体   繁体   English

如何在其他综合浏览量中显示两个不同的部分?

[英]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.

相关问题 如何渲染两个具有相同名称的不同部分? - How do I render two different partials with the same name? Rails-错误后,我可以在视图中显示不同的部分吗? - Rails - Can I Show Different Partials in a View After an Error? Hotwire:如何将广播的 object 的两个部分渲染到两个不同的页面上? - Hotwire: How to render two partials for a broadcasted object on to two different pages? 如何将路线连接到部分? - How do I connect routes to partials? 如何以两种不同的方式定义两个相互关联的模型之间的ActiveRecord关系? - How do I define ActiveRecord relationships between two models that relate to each other in two different ways? 我希望同时显示两个不同模型的“显示”动作; 我该怎么做? - I wish to display a "show" action for two different models at the same; how do I go about this? Railtie(宝石):我如何包含和渲染erb partials? - Railtie(gem): How do i include and render erb partials? 如何编写规范以验证局部渲染? - How do I write a spec to verify the rendering of partials? 如何仅使用部分创建4x4网格? - How do I create a 4x4 grid with partials only? 如何对字符串上的每个其他字符执行操作? - How do I perform an operation on every other character on a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM