如何用祖先宝石创建一棵树

时间:2015-07-22 09:32:05

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

我正在创建一个在线零售店。

尝试使用Ancestry Gem创建一个类别树。

我使用本教程作为指南,甚至使用脚手架。 https://www.youtube.com/watch?v=wDLn0V_AYgs

我如何制作一棵树?我已经阅读了Gem的github页面,但我仍然感到困惑。

我只需要将index.html.erb视图看起来像这样。

目前我的树看起来像这样。

enter image description here

控制器。

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

  # GET /categories
  # GET /categories.json
  def index
    @categories = Category.all
  end

  # GET /categories/1
  # GET /categories/1.json
  def show
  end

  # GET /categories/new
  def new
    @category = Category.new
  end

  # GET /categories/1/edit
  def edit
  end

  # POST /categories
  # POST /categories.json
  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

  # PATCH/PUT /categories/1
  # PATCH/PUT /categories/1.json
  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

  # DELETE /categories/1
  # DELETE /categories/1.json
  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
    # Use callbacks to share common setup or constraints between actions.
    def set_category
      @category = Category.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:name, :parent_id)
    end
end

模型

class Category < ActiveRecord::Base
    has_ancestry
end

查看index.html.erb

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

<h1>Listing Categories</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Ancestry</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @categories.each do |category| %>
      <tr>
        <td><%= category.name %></td>
        <td><%= category.ancestry %></td>
        <td><%= link_to 'Show', category %></td>
        <td><%= link_to 'Edit', edit_category_path(category) %></td>
        <td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Category', new_category_path %>

Rails控制台。 Category.last

 Category Load (0.2ms)  SELECT  "categories".* FROM "categories"  ORDER BY "categories"."id" DESC LIMIT 1
 => #<Category id: 6, name: "HTC", ancestry: "1", created_at: "2015-07-21 10:45:54", updated_at: "2015-07-21 11:06:20"> 

0 个答案:

没有答案