mardi 28 juillet 2015

SELECT "categories".* FROM "categories" WHERE "categories"."ancestry" IS NULL

I made a Categories model using Ancestry gem.

When i try to create a new Item I receive this error. Im creating the item in my views/items/New template

ERROR IS FROM THE CONSOLE. In the browser it just says "Your item didn't save"

SELECT "categories".* FROM "categories" WHERE "categories"."ancestry" IS NULL

Anyone know how to this error fix? I've tried for a few hours can't figure it out.

views/items/new.html.erb

<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
          <%= simple_form_for @item, html: { multipart: true } do |f| %>
            <%= f.input :image%>
            <%= f.collection_select :category, Category.order(:name), :id, :name, include_blank: true, :prompt => "Select One Category" %>
            <%= f.input :title%>
            <%= f.input :price %>
            <%= f.input :description %>
            <%= f.button :submit, "Create new item", class: "btn btn-primary" %>
          <% end %>
        </div>
      </div>    
    </div>
  </div>
</div>

Item.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, :categories, :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

Category Model

class Category < ActiveRecord::Base
    has_ancestry
    has_many :items
end

Item Model

class Item < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    validates :category, presence: true
    validates :title, presence: true, length: { maximum: 30 } 
    validates :price, presence: true
    validates :description, presence: true, length: { maximum: 2000 }
    validates :user_id, presence: true
    has_attached_file :image, styles: { large: "600x600", medium: "250x250", thumb:"100x100#"}
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

Categories Controller

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

Aucun commentaire:

Enregistrer un commentaire