mardi 1 septembre 2015

Turn A String Of Clothes Sizes "xs,s,m,l,xl" into some you can select

Hello I have an Items Model and a Categories Model. When creating a Shirt category you can create all the sizes you expect a Shirt category to have.

At the moment the sizes are a string "XS, S, M, L, XL, XLL"

How do i make it so its something you can select?

For instance when you create an Yellow Shirt item you might only have XS and L available.

See below

2.2.1 :002 > c = Category.last
  Category Load (0.3ms)  SELECT  "categories".* FROM "categories"  ORDER BY "categories"."id" DESC LIMIT 1
 => #<Category id: 14, name: "Shirt", ancestry: nil, created_at: "2015-09-01 10:09:32", updated_at: "2015-09-01 10:09:32", sizes: "XS, S, M, L, XL, XLL">
2.2.1 :003 > c
 => #<Category id: 14, name: "Shirt", ancestry: nil, created_at: "2015-09-01 10:09:32", updated_at: "2015-09-01 10:09:32", sizes: "XS, S, M, L, XL, XLL">
2.2.1 :004 > c.sizes
 => "XS, S, M, L, XL, XLL"

Categories 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
  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, :sizes, :parent_id)
    end

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

end

Categories Form

<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 %>
          <%= f.input :sizes %>
          <%= f.collection_select :parent_id, Category.order(:name), :id, :name, include_blank: true %>
          </div>
          <div class="form-actions">
          <%= f.button :submit %>
          </div>
          <% end %>
        </div>
      </div>    
    </div>
  </div>
</div>

Incase you need the Items view

<h1>Create New item</h1>

<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_id, Category.order(:name), :id, :name, include_blank: true, :prompt => "Select One Category" %>
            <%= f.input :tag_list %>
            <%= 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>

Migration for turning sizes into an array.

class AddSizesToCategories < ActiveRecord::Migration
  def change
    add_column :categories, :sizes, :string, array: true, default: []
  end
end

Aucun commentaire:

Enregistrer un commentaire