[英]Rails Shopping Cart - not adding to current order
Rails noob here. Rails noob在这里。
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. 没有改变任何代码(我git reset - 硬到我工作的上一次提交)它就破了。 (?!?) Here's the breakdown: (?!?)这是细分:
Github Repo: https://github.com/christinecha/michaka Github Repo: https : //github.com/christinecha/michaka
Creates a product. 创建一个产品。 ✓ ✓
Adds Product ID to a new Order Item. 将产品ID添加到新订单项目。 ✓ ✓
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 - 购物车未连接到正确的订单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 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
It looks like ProductsController#create is called twice, once with format html and once as json. 看起来像ProductsController #create被调用两次,一次是格式化html,一次是json。
I think you're submitting some of your data via ajax but still doing a post request from the form. 我认为您通过ajax提交了一些数据,但仍然从表单中提交了一个帖子请求。 However your controller, in it's format.html
response is redirecting before all of the javascript actions have completed. 但是你的控制器,在它的format.html
响应中,在所有的javascript动作完成之前重定向。
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. 由于您只保存@order并从您的初始ajax()成功后由js调用的OrderItemsController #create设置会话,因此在收到重定向时它是不完整的。
What I think happens on click: 我认为点击时会发生什么:
I would suggest either redesigning the submit process to submit through regular form post or entirely through js. 我建议重新设计提交流程,通过常规表格或完全通过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';"
例如,您可以从表单中禁用post并将OrderItemsController #create更改为最终重定向(通过js) render :js => "window.location.href = '/cart';"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.