Rails Shopping Cart - not adding to current order

為{幸葍}努か 提交于 2020-01-02 06:10:12

问题


Rails noob here.

I'm building a basic shopping cart and it was working perfectly before. Without changing any code (I git reset --hard to my prev commit where it was working) it broke. (?!?) Here's the breakdown:

Github Repo: https://github.com/christinecha/michaka

  • Creates a product. ✓

  • Adds Product ID to a new Order Item. ✓

  • Adds Order Item to an Order. ✓

--

Possible Issues

! - New Orders keep being created as you create Order Items = cart is always empty.

! - Cart is not connecting to the right Order ID

! - New sessions are being triggered = new Orders = problem

--

ORDER ITEMS CONTROLLER

    class OrderItemsController < ApplicationController
  def create
    @order = current_order
    @order_item = @order.order_items.new(order_item_params)
    @order.save
    session[:order_id] = @order.id
  end

  def update
    @order = current_order
    @order_item = @order.order_items.find(params[:id])
    @order_item.update_attributes(order_item_params)
    @order_items = @order.order_items
  end

  def destroy
    @order = current_order
    @order_item = @order.order_items.find(params[:id])
    @order_item.destroy
    @order_items = @order.order_items
  end
private
  def order_item_params
    params.require(:order_item).permit(:quantity, :product_id)
  end
end

SESSION_STORE.RB

Rails.application.config.session_store :cookie_store, key: '_bead-project_session'

ORDER MODEL

class Order < ActiveRecord::Base
  belongs_to :order_status
  has_many :order_items
  before_create :set_order_status
  before_save :update_subtotal

  def subtotal
    order_items.collect { |oi| oi.valid? ? (oi.quantity * oi.unit_price) : 0 }.sum
  end

  def subtotal_cents
    subtotal * 100
  end


private
  def set_order_status
    self.order_status_id = 1
  end

  def update_subtotal
    self[:subtotal] = subtotal
  end

end

APPLICATION CONTROLLER

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  helper_method :current_order

  def current_order
    if !session[:order_id].nil?
      Order.find(session[:order_id])
    else
      Order.new
    end
  end
end

回答1:


It looks like ProductsController#create is called twice, once with format html and once as json.

I think you're submitting some of your data via ajax but still doing a post request from the form. However your controller, in it's format.html response is redirecting before all of the javascript actions have completed.

Since you only save @order and set the session from OrderItemsController#create which is called by js after your initial ajax().success, it is incomplete when the redirect is received.

What I think happens on click:

  1. ajax post request AND regular form post
  2. ajax success -> submit #order_item_product_id form
  3. redirected by original form post response

I would suggest either redesigning the submit process to submit through regular form post or entirely through js. For example you could disable post from the form and change OrderItemsController#create to finally redirect (via js) render :js => "window.location.href = '/cart';"



来源:https://stackoverflow.com/questions/32464758/rails-shopping-cart-not-adding-to-current-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!