mardi 21 juillet 2015

Ancestry gem not working - Child Category not linking to Parent Category

Im creating an online retail store so i need a Category model. Im using the Ancestry gem for this.

I have followed this tutorial https://www.youtube.com/watch?v=wDLn0V_AYgs

Problem is this. When i create a child category such as below. It doesn't seem to link to the parrent specified. HTC should link to Mobile Phones. But it doesn't. In the Listing Categories Image next to HTC there should be the ID of Mobile Phones. So its not linking.

Anyone know how to fix?

enter image description here enter image description here Here is my _form.html.erb

<%= simple_form_for(@category) do |f| %>


  <div class="form-inputs">
    <%= f.input :name %>
    <%= f.collection_select :parent_id, Category.order(:name), :id, :name, include_blank: true %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

Here is model.

class Category < ActiveRecord::Base
    has_ancestry
end

Here is my controller

**class ItemsController < ApplicationController
  before_action :correct_user_edit,   only: [:edit, :update, :destroy]

  def index
    @item = @user.items.paginate(page: params[:page])
  end

  def new
    @item = Item.new
  end

  def home
    @items = Item.paginate(page: params[:page])
  end

  def edit
    @item = Item.find(params[:id])
    @user = User.find(params[:id])
  end

  def show
    @item = Item.find(params[:id])
  end

  def update
    @item = Item.find(params[:id])
    if @item.update(item_params)
       redirect_to @item
       flash[:success] = 'Item was successfully updated.'
    else
      render "edit"
    end
  end

  def create
    @item = current_user.items.build(item_params)
    if @item.save
      redirect_to @item
      flash[:success] = "You have created a new item"
    else
      flash[:danger] = "Your item didn't save"
      render "new"
    end
  end

  def destroy
    Item.find(params[:id]).destroy
    flash[:success] = "Item deleted"
    redirect_to users_url
  end

  private

    def item_params
      params.require(:item).permit(:title, :price, :description, :image)
    end

    #Check to see if user can edit item.
    def correct_user_edit
      if @item = current_user.items.find_by(id: params[:id])
      else
        flash[:danger] = "You can't edit that item"
        redirect_to root_url if @item.nil?
      end
    end

end

Aucun commentaire:

Enregistrer un commentaire