.in_groups_of(4, false) 错误

.in_groups_of(4, false) error

你好,我有一些类别正在尝试使用 in_groups_of rails 方法显示

http://api.rubyonrails.org/classes/Array.html#method-i-in_groups_of

我收到此错误,但我不明白为什么。我只学了 rails 3 个月。

NoMethodError in Categories#show
undefined method `name' for #<Array:0x007fe6d3adf360>

    <% @category.children.in_groups_of(4, false) do |category| %>
      <li> 
        <%= link_to_unless_current category.name, category %> 
     </li>
    <% end %>
</ul>

我正在使用 Acestry Gem,因此名称方法是 gem 的一部分。 https://github.com/stefankroes/ancestry

这是我的显示视图。很简单

    <p id="notice"><%= notice %></p>

    <ul>
        <% @category.ancestors.each do |category| %>
          <%= link_to category.name, category %> &gt;
        <% end %>
    </ul>

    <h2>
      <strong><%= @category.name %></strong>
    </h2>

    <h2>
      <strong><%= @category.name %></strong>
    </h2>
    <ul>
        <% @category.children.in_groups_of(4, false) do |category| %>
          <li> 
            <%= link_to_unless_current category.name, category %> 
         </li>
        <% end %>
    </ul>

类别控制器。

class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]

  def index
    @categories = Category.all
  end

  def show
  end

  def new
    @category = Category.new
  end

  def edit
  end

  def create
    @category = Category.new(category_params)

    respond_to do |format|
      if @category.save
        format.html { redirect_to @category, notice: 'Category was successfully created.' }
        format.json { render :show, status: :created, location: @category }
      else
        format.html { render :new }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { render :show, status: :ok, location: @category }
      else
        format.html { render :edit }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

    def set_category
      @category = Category.find(params[:id])
    end

    def category_params
      params.require(:category).permit(:name, :parent_id)
    end
end
<% @category.children.in_groups_of(4, false) do |children| %>
   <% children.each do |child|%>
      <li> 
        <%= link_to_unless_current child.name, child %> 
     </li>
    <% end %>
<% end %>

要使链接在转到下一行之前显示为 4 列,请执行此操作。

<table>  
    <% @category.children.in_groups_of(4, false) do |children| %>  
    <tr>  
      <% children.each do |category| %>  
      <td><%= link_to_unless_current category.name, category %></td>  
      <% end %>  
    </tr>  
    <% end %>  
</table>