Rails:in_groups_of - 错误

时间:2015-09-26 05:05:17

标签: ruby-on-rails

我想弄清楚如何在我的视图中链接到我的文章,并且卡在' in_groups_of'调用

我在视图中有以下部分:

<div class="row">
  <div class="col-md-10 col-md-offset-1">
    <% Article.in_groups_of(2) do |group| %>
      <div class="row">
        <% group.compact.each do |article| %>
            <div class="col-md-5 col-md-offset-1">
              <div class="indexdisplay">
              <%= image_tag article.image_url, width: '100%', height: '200px' if article.image.present? %>
                  <div class="indexheading"> <%= link_to article.title, article %> </div>
                <div class="indexsubtext">    <%= truncate(article.body, :ommission => "...", :length => 250) %></div>
                 </div>
            </div>
              <% end %>
              </div>

    <% end %>

  </div>
</div>

当我尝试渲染它时,我收到此错误:

NoMethodError at /
undefined method `in_groups_of' for #<Class:0x007fa70300cda8>

任何人都可以看到错误吗?

我尝试将文章更改为文章(我的模型文件是article.rb),但我收到此错误:

NameError at /
uninitialized constant ActionView::CompiledTemplates::Articles

1 个答案:

答案 0 :(得分:0)

<div class="row">
  <div class="col-md-10 col-md-offset-1">
    <% Article.all.in_groups_of(2) do |group| %>
      <div class="row">
        <% group.compact.each do |article| %>
            <div class="col-md-5 col-md-offset-1">
              <div class="indexdisplay">
              <%= image_tag article.image_url, width: '100%', height: '200px' if article.image.present? %>
                  <div class="indexheading"> <%= link_to article.title, article %> </div>
                <div class="indexsubtext">    <%= truncate(article.body, :ommission => "...", :length => 250) %></div>
                 </div>
            </div>
              <% end %>
              </div>

    <% end %>

  </div>
</div>

试试这个。