mardi 24 novembre 2015

Update Attributes For Nested Forms

Hello I'm creating an online retail store.

I have a Category model and a Sizes model. They are nested in a form. When I create a Category I also create sizes for that category.

Right now I can create a Category and sizes. However I can't update the sizes in the nested form.

So click edit category and change the name of one size then click update. I get the below error. On screen it just says "has already been taken".

How do I update sizes through this nested form? /Users/Documents/Safsy/Website/Safsy/Safsy/app/controllers/categories_controller.rb @ line 40 CategoriesController#update:

    39: def update
 => 40:   binding.pry
    41:   if @category.update(category_params)
    42:      redirect_to @category
    43:      flash[:success] = 'Category was successfully updated.'
    44:   else
    45:     render "edit"
    46:   end
    47: end

[1] pry(#<CategoriesController>)>
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameters: _destroy, id
Unpermitted parameter: _destroy
Unpermitted parameter: _destroy
Unpermitted parameter: _destroy
Unpermitted parameter: _destroy
Unpermitted parameter: _destroy
Unpermitted parameter: _destroy
Unpermitted parameter: _destroy
   (0.1ms)  begin transaction
  Category Load (0.1ms)  SELECT  "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1  [["id", 64]]
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XSmall' LIMIT 1
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'Small' LIMIT 1
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'Medium' LIMIT 1
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'Large' LIMIT 1
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XL' LIMIT 1
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XXL' LIMIT 1
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XXXL' LIMIT 1
  CACHE (0.0ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XSmall' LIMIT 1
  CACHE (0.0ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'Small' LIMIT 1
  CACHE (0.0ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'Medium' LIMIT 1
  CACHE (0.0ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'Large' LIMIT 1
  CACHE (0.0ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XL' LIMIT 1
  CACHE (0.0ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XXL' LIMIT 1
  Size Exists (0.1ms)  SELECT  1 AS one FROM "sizes" WHERE "sizes"."title" = 'XXXL test' LIMIT 1
  Category Exists (0.1ms)  SELECT  1 AS one FROM "categories" WHERE ("categories"."name" = 'Shorts' AND "categories"."id" != 68) LIMIT 1
   (0.1ms)  rollback transaction

Category Model:

class Category < ActiveRecord::Base
  has_ancestry

  has_many :products
  has_many :sizes

  validates :name, presence: true, length: { maximum: 20 }, uniqueness: true

  accepts_nested_attributes_for :sizes, allow_destroy: true
end

Sizes Model:

class Size < ActiveRecord::Base
    validates :title, presence: true, length: { maximum: 15 }
    validates :title, uniqueness: true

  belongs_to :category
end

Category controller:

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

  def index
    @categories = Category.all
  end

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

  def new
    @category = Category.new
    3.times do
      @category.sizes.build
    end
  end

  def edit
  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
    binding.pry
    if @category.update(category_params)
       redirect_to @category
       flash[:success] = 'Category was successfully updated.'
    else
      render "edit"
    end
  end

  def destroy
    category = Category.find(params[:id])
    category.sizes.destroy_all
    category.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, :parent_id, size_ids: [], sizes_attributes: [:title])
  end
end

Sizes Controller:

class SizesController < ApplicationController
  before_action :logged_in_user, only: [:create, :index, :destroy, :update]
  before_action :admin_user, only: [:create, :index, :destroy, :update]

  def create
    @size = Size.create(size_params)
  end

  def index
    @sizes = Size.all
  end

  def destroy
    Size.find(params[:id]).destroy
  end

  def update
    @size.update_attributes(size_params)
  end

  private

  def size_params
    params.require(:size).permit(:title, :category_id)
  end
end

Here are the params at Category Update method:

    39: def update
 => 40:   binding.pry
    41:   if @category.update(category_params)
    42:      redirect_to @category
    43:      flash[:success] = 'Category was successfully updated.'
    44:   else
    45:     render "edit"
    46:   end
    47: end

[1] pry(#<CategoriesController>)> params
=> {"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"ZHuWURBwIctFJLgZ1HGNeKvGpK8LlgH9S6Mrh7No/CFdjtwNFoNtfi9NIVHBVhApYH/U5tuOzV0eqhSq/95SCw==",
 "category"=>
  {"name"=>"Shorts",
   "parent_id"=>"64",
   "sizes_attributes"=>
    {"0"=>{"title"=>"XSmall", "_destroy"=>"false", "id"=>"21"},
     "1"=>{"title"=>"Small", "_destroy"=>"false", "id"=>"22"},
     "2"=>{"title"=>"Medium", "_destroy"=>"false", "id"=>"23"},
     "3"=>{"title"=>"Large", "_destroy"=>"false", "id"=>"24"},
     "4"=>{"title"=>"XL", "_destroy"=>"false", "id"=>"25"},
     "5"=>{"title"=>"XXL", "_destroy"=>"false", "id"=>"26"},
     "6"=>{"title"=>"XXXL test", "_destroy"=>"false", "id"=>"27"}}},
 "commit"=>"Update Category",
 "controller"=>"categories",
 "action"=>"update",
 "id"=>"68"}

Aucun commentaire:

Enregistrer un commentaire