Ruby on rails 如何在每次更改类时遍历Rails中的每个帖子
我试图在Rails应用程序中循环浏览每一篇文章,其中类每次都会更改。这是HTML中的布局,我如何在rails循环中创建它。css类从样式1到样式6Ruby on rails 如何在每次更改类时遍历Rails中的每个帖子,ruby-on-rails,Ruby On Rails,我试图在Rails应用程序中循环浏览每一篇文章,其中类每次都会更改。这是HTML中的布局,我如何在rails循环中创建它。css类从样式1到样式6 <article class="style1"> <span class="image"> <img src="images/pic01.jpg" alt="" /> </span> <a href="generic.html"> <
<article class="style1">
<span class="image">
<img src="images/pic01.jpg" alt="" />
</span>
<a href="generic.html">
<h2>Magna</h2>
<div class="content">
<p>Sed nisl arcu euismod sit amet nisi lorem etiam dolor veroeros et feugiat.</p>
</div>
</a>
</article>
<article class="style2">
<span class="image">
<img src="images/pic02.jpg" alt="" />
</span>
<a href="generic.html">
<h2>Lorem</h2>
<div class="content">
<p>Sed nisl arcu euismod sit amet nisi lorem etiam dolor veroeros et feugiat.</p>
</div>
</a>
</article>
等等。您可以使用
每个带有索引的\u
类似于:
- @posts.each_with_index do |post, i|
%article{class: "style#{(i % 6) + 1}"}
%span.image
...
对不起,那是在哈姆尔而不是再培训局。我不再使用ERB了,我已经忘记了它是如何运行的。但是,希望这能让您了解如何使用每个带有索引的\u
。你可以在网上阅读更多
雇员再培训局例子:
<%- @posts.each_with_index do |post, i| %>
<article class="style<%= (i % 6) + 1 %>">
<span class="image">
...
</span>
</article>
<% end %>
...
您也可以修改CSS选择器:
article:nth-child(6n + 1){ color: peachpuff; }
article:nth-child(6n + 2){ color: deepskyblue; }
article:nth-child(6n + 3){ color: dimgray; }
article:nth-child(6n + 4){ color: forestgreen; }
article:nth-child(6n + 5){ color: gold; }
article:nth-child(6n + 6){ color: coral; }
解释如下:
这里的示例:您正在搜索cycle helper
<% @posts.each do |post| %>
<%= content_tag :div, class: cycle('style1', 'style2', 'style3') do %>
...
<% end %>
<% end %>
...
您可以在此处找到更多信息:编辑以添加ERB示例这不是Rails的方法。这是自行车助手。看起来有点神秘。但是,是的。好啊