Rails 4:递增重复项

时间:2014-07-19 09:57:35

标签: ruby-on-rails

想在我的购物应用中增加重复的项目 因此,当用户下订单然后想要编辑订单时,他可以更改订单数量。

当我点击数量时(我已将数量设置为1)它进入order_itmes编辑屏幕 - 它允许我更新订单但是当我点击提交时收到错误

OrderItemsController中的NameError#update

@order_item = OrderItem.find(params[:id])
    respond_to do |format|
      if order_item.params[:quantity].to_i == 0   **<-----Error**
        @order_item.destroy
        format.html { redirect_to @order_item.order, notice: 'Item was deleted from your cart.' }
        format.json { head :no_content }

参数

{"utf8"=>"✓",
 "_method"=>"patch",
 "authenticity_token"=>"/8xRjbtusgdLV5SVQP55aUDccYdUzB9G23heTtEeNfk=",
 "order_item"=>{"quantity"=>"2"},
 "commit"=>"Update Order item",
 "id"=>"68"}

order_items控制器

class OrderItemsController < ApplicationController
  before_action :set_order_item, only: [:show, :edit, :destroy]
  before_action :load_order, only: [:create]


  # GET /order_items/1/edit
  def edit
  end

  # POST /order_items
  # POST /order_items.json
  def create
       @order_item = @order.order_items.find_or_initialize_by_product_id(params[:product_id])
       @order_item.quantity += 1

    respond_to do |format|
      if @order_item.save
        format.html { redirect_to @order, notice: 'Successfully Added Product To Cart.' }
        format.json { render action: 'show', status: :created, location: @order_item }
      else
        format.html { render action: 'new' }
        format.json { render json: @order_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /order_items/1
  # PATCH/PUT /order_items/1.json
  def update
    @order_item = OrderItem.find(params[:id])
    respond_to do |format|
      if order_item.params[:quantity].to_i == 0
        @order_item.destroy
        format.html { redirect_to @order_item.order, notice: 'Item was deleted from your cart.' }
        format.json { head :no_content }
      elsif @order_item.update(order_item_params)
        format.html { redirect_to @order_item.order, notice: 'Successfully updated the order item.' }
      else 
        format.html { render action: 'edit' }
        format.json { render json: @order_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /order_items/1
  # DELETE /order_items/1.json
  def destroy
    @order_item.destroy
    respond_to do |format|
      format.html { redirect_to @order_item.order }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_order_item
      @order_item = OrderItem.find(params[:id])
    end

    def load_order
        @order = Order.find_or_initialize_by_id(session[:order_id], status: "Unsubmitted", user_id: session[:user_id])

        if @order.new_record?
          @order.save!
          session[:order_id] = @order.id
        end
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_item_params
      params.require(:order_item).permit(:product_id, :order_id, :quantity)
    end
end

order.controller

class OrdersController < ApplicationController
  before_action :set_order, only: [:show, :edit, :update, :destroy, :confirm]

  # GET /orders
  # GET /orders.json
  def index
    @orders = Order.all
  end

  # GET /orders/1
  # GET /orders/1.json
  def show
  end

  # GET /orders/new
  def new
    @order = Order.new
  end

  # GET /orders/1/edit
  def edit
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)

    respond_to do |format|
      if @order.save
        format.html { redirect_to @order, notice: 'Order was successfully created.' }
        format.json { render action: 'show', status: :created, location: @order }
      else
        format.html { render action: 'new' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /orders/1
  # PATCH/PUT /orders/1.json
  def update
    respond_to do |format|
      if @order.update(order_params.merge(status: 'submitted'))
        format.html { redirect_to confirm_order_path(@order), notice: 'Order was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.json
  def destroy
    @order.destroy
    respond_to do |format|
      format.html { redirect_to products_path }
      format.json { head :no_content }
    end
  end

  def confirm
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_order
      @order = Order.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
      params.require(:order).permit(:user_id, :status, :address_id)
    end
end

order_item.rb

class OrderItem < ActiveRecord::Base

  belongs_to :order
  belongs_to :product

  validates :order_id, :product_id, presence: true
  validates :quantity, numericality: { only_integer: true, greater_than: 0 }

  def subtotal
    quantity * product.price
  end
end

1 个答案:

答案 0 :(得分:0)

刚看到它如果order_item.params [:quantity] .to_i == 0

应该是order_item_params [:quantity] .to_i == 0