jeudi 3 septembre 2015

Sizes Not Showing In Category- Category.sizes: nill

Hello im creating an online clothes store.

I have a Category Model. When you create a Category you must select the sizes that category will have. Like a Shirt Category will have XS, S, M, L, XL, XXL.

I created all sizes of clothes using enum sizes: which is in my Category model.

For some reason when i run run Category.last it shows sizes.nill. How do i make this work?

2.2.1 :003 > Category.last
  Category Load (0.4ms)  SELECT  "categories".* FROM "categories"  ORDER BY "categories"."id" DESC LIMIT 1
 => #<Category id: 25, name: "test", ancestry: "20", created_at: "2015-09-03 11:27:40", updated_at: "2015-09-03 11:27:40", sizes: nil>

Category Controller

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

  def index
    @categories = Category.all
  end

  def show
    @tags = Item.where(category_id: @category.id).tag_counts_on(:tags)
    if params[:tag]
      @items = Item.tagged_with(params[:tag])
    else
      @items = Item.where(category_id: @category.id).order("created_at DESC")
    end
  end

  def new
    @category = Category.new
  end

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

  def create
    @category = Category.new(category_params)
    if @category.save
      redirect_to @category
      flash[:success] = "You have created a new category"
    else
      flash[:danger] = "Your category didn't save"
      render "new"
    end
  end

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

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

  private

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

    def category_params
      params.require(:category).permit(:name, :sizes, :parent_id)
    end

    # Confirms an admin user.
    def admin_user
      redirect_to(root_url) unless current_user.try(:admin?)
    end

end

Category Model

class Category < ActiveRecord::Base
    has_ancestry
    has_many :items
    validates :name, presence: true, length: { maximum: 20 }
    enum sizes: [:XSmall, :Small, :Medium, :Large, :XLarge, :XXLarge, 
                             :'1', :'2', :'3', :'4', :'5', :'6', :'7', :'8', :'9', :'10', 
                             :'11', :'12', :'13', :'14', :'15', :'16', :'17', :'18', :'19', :'20',
                             :'21', :'22', :'23', :'24', :'25', :'26', :'27', :'28', :'29', :'30',
                             :'31', :'32', :'33', :'34', :'35', :'36', :'37', :'38', :'39', :'40',
                            ]
end

Category Create View

<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(@category) do |f| %>
                      <div class="form-inputs">
                        <%= f.input :name %>
                        <div class ="form-inline">
                            <%= f.input :sizes, collection: Category.sizes.keys, as: :check_boxes, input_html: { multiple: true }, label:"Select Size" %>
                        </div>
                        <%= f.collection_select :parent_id, Category.order(:name), :id, :name, {prompt: "Select Parrent ID If Applicable"},include_blank: true %>
                      </div>
                      <div class="form-actions">
                        <%= f.button :submit %>
                      </div>
                    <% end %>
        </div>
      </div>    
    </div>
  </div>
</div>

My migration for sizes in category.

class AddSizesToCategories < ActiveRecord::Migration
  def change
    add_column :categories, :sizes, :integer, array: true
  end
end

Schema

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.string   "ancestry"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "sizes"
  end

Aucun commentaire:

Enregistrer un commentaire