4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails + erb、slimを使ってdiv要素を3つずつrowで囲む

4
Posted at

Bootstrapでお馴染みのこちらの構文。

<div class="row">
  <div class="col-md-4">
    <div class="item">item1</div>
  </div>
  <div class="col-md-4">
    <div class="item">item2</div>
  </div>
  <div class="col-md-4">
    <div class="item">item3</div>
  </div>
</div>

HTML直書きなら簡単ですが、Railsで@items = Item.allみたいのをこの書式で表示しようとしたときにちょっと躓きました。

結論、これでいけます。

erb
<% @items.each_slice(3) do |group| %>
  <div class="row">
    <% group.each do |item| %>
      <div class="col-md-4">
        <%= item %>
      </div>
    <% end %>
  </div>
<% end %>
slim
- @items.each_slice(3) do |group|
  .row
    - group.each do |item|
      .col-md-4
        = item
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?