Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails中的高级stylsheet_Ruby On Rails_Css_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails rails中的高级stylsheet

Ruby on rails rails中的高级stylsheet,ruby-on-rails,css,ruby-on-rails-4,Ruby On Rails,Css,Ruby On Rails 4,我想为我的博客实现一个类似的设计,所以对于我在视图中的第一篇文章class=“first post”现在我想区分第三个5 7。。。来自2.4。。发布我如何做到这一点?为清晰和简单起见进行了编辑: <% @posts.each_with_index do |p, index| %> <% position = index+1 %> <% if position == 1 then %> FIRST <% elsif (positio


我想为我的博客实现一个类似的设计,所以对于我在视图中的第一篇文章
class=“first post”
现在我想区分第三个5 7。。。来自2.4。。发布我如何做到这一点?

为清晰和简单起见进行了编辑:

<% @posts.each_with_index do |p, index| %>

  <% position = index+1 %>

  <% if position == 1 then %>
    FIRST
  <% elsif (position.even?) then %>
    EVEN
  <% else %>
    ODD
  <% end %>

<% end %>

第一
甚至
奇怪的

您可以
弹出
阵列中的第一个对象:

post = @posts.pop
然后你可以用它做你喜欢的事。可能会渲染一个部分。然后你可以在剩下的帖子上循环。如果需要粒度控制,可以使用数组
切片

<% @posts.each_slice(2) do |slice| %>
  <div class="row">
    <% slice.each do |post| %>
      <div class="item">
        show item here
      </div>
    <% end %>
  </div>
<% end %>

在此处显示项目
这将把阵列分成两组,让您对布局有更精确的控制

或者,建议对msanteler的答案进行改进:

<% @posts.each_with_index do |post, i| %>
  <% if i == 0 %>
    show stuff
  <% elsif i.even? %>
    show stuff
  <% else %>
    show stuff
  <% end %>
<% end %>

表演材料
表演材料
表演材料

很好的调用
。即使?
只需注意
索引
以0开头,而不是1