.in_groups_of(4,false)错误

时间:2015-07-24 11:30:12

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4

您好我有一些类别,我试图使用in_groups_of rails方法显示

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

我收到此错误,我无法弄清楚原因。我只学习了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,因此name方法是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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

要使链接在转到下一行之前显示在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>