因此,我阅读了“使用Rails进行敏捷Web开发”这本书,在书中的"Depot“应用程序的基础上,我在Heroku上运行了一个功能强大的web应用程序。我主要是在编辑CSS/SASS,我做的事情弄乱了我购物车里的line_item数量。
不管我做了什么,我都搞不清楚。现在,当我点击主页上的“添加到购物车”按钮时,有两个商品被添加到购物车中。这跟我的数据库有关系吗?在photoshop/CSS-land中失去了一点流量后,我完全被搞糊涂了。
所以简而言之:我点击“添加到购物车”按钮,我点击的每个项目都会得到两个。
这是我的购物车控制器:
class CartsController < ApplicationController
skip_before_filter :authorize, only: [:create, :update, :destroy]
# GET /carts
# GET /carts.json
def index
@carts = Cart.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @carts }
end
end
# GET /carts/1
# GET /carts/1.json
def show
begin
@cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
else
respond_to do |format|
format.html #show.html.erb
format.json { render json: @cart }
end
end
end
# GET /carts/new
# GET /carts/new.json
def new
@cart = Cart.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @cart }
end
end
# GET /carts/1/edit
def edit
@cart = Cart.find(params[:id])
end
# POST /carts
# POST /carts.json
def create
@cart = Cart.new(params[:cart])
respond_to do |format|
if @cart.save
format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
format.json { render json: @cart, status: :created, location: @cart }
else
format.html { render action: "new" }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# PUT /carts/1
# PUT /carts/1.json
def update
@cart = Cart.find(params[:id])
respond_to do |format|
if @cart.update_attributes(params[:cart])
format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @cart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /carts/1
# DELETE /carts/1.json
def destroy
@cart = current_cart
@cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url }
format.json { head :no_content }
end
end
end下面是我的models/cart.rb文件:
class Cart < ActiveRecord::Base
attr_accessible :title, :body, :name, :quantities_attributes, :quantities, :quantity, :product_id, :line_items
#the addition of everything in attr_accessible after :body may be unnecessary if bugs show up later on...
has_many :line_items, dependent: :destroy
def add_product(product_id)
current_item = line_items.find_by_product_id(product_id)
if current_item
current_item.quantity += 1
else
current_item = line_items.build(product_id: product_id)
end
current_item
end
def total_price
line_items.to_a.sum { |item| item.total_price }
end
end这是我的create.js.erb文件
$("#notice").hide();
if ($('#cart tr').length == 1) { $('#cart').show('blind', 1000); }
$('#cart').html("<%=j render @cart %>");
$('#current_item').css({'background-color':'#fb0f19'}).
animate({'background-color':'#08BBD1'}, 1000);这是我的application_controller.rb:
class ApplicationController < ActionController::Base
before_filter :authorize
protect_from_forgery
private
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
protected
def authorize
unless User.find_by_id(session[:user_id])
redirect_to login_url, notice: "Please log in"
end
end
end这是我的视图/购物车/_cart.html.erb
<% unless cart.line_items.empty? %>
<div class="cart_title">Your Comic Cart</div>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= cart.total_price %> BTC</td>
</tr>
</table>
<%= button_to "Checkout", new_order_path, method: :get %>
<%= button_to 'Empty cart', cart, method: :delete,
confirm: 'Are you sure?' %>
<% end %>line_items_controller.rb:
class LineItemsController < ApplicationController
skip_before_filter :authorize, only: :create
# GET /line_items
# GET /line_items.json
def index
@line_items = LineItem.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @line_items }
end
end
# GET /line_items/1
# GET /line_items/1.json
def show
@line_item = LineItem.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @line_item }
end
end
# GET /line_items/new
# GET /line_items/new.json
def new
@line_item = LineItem.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @line_item }
end
end
# GET /line_items/1/edit
def edit
@line_item = LineItem.find(params[:id])
end
# POST /line_items
# POST /line_items.json
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to store_url }
format.js { @current_item = @line_item }
format.json { render json: @line_item,
status: :created, location: @line_item }
else
format.html { render action: "new" }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
# PUT /line_items/1
# PUT /line_items/1.json
def update
@line_item = LineItem.find(params[:id])
respond_to do |format|
if @line_item.update_attributes(params[:line_item])
format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
# DELETE /line_items/1
# DELETE /line_items/1.json
def destroy
@line_item = LineItem.find(params[:id])
@line_item.destroy
respond_to do |format|
format.html { redirect_to line_items_url }
format.json { head :no_content }
end
end
end我不确定是否还有更多值得展示的文件,现在已经很晚了,我完全迷路了,所以我可能应该等到早上再发这篇文章。如果其他人遇到这种情况,请告诉我。我仍然很难使用数据库,所以我可能完全找不到合适的地方。谢谢你的帮助,我知道这个错误在我的解释中缺乏重点。
发布于 2014-02-18 01:02:51
我想问题出在你的store.js.coffee上。根据下面链接中的勘误表153检查和修改代码:
http://pragprog.com/titles/rails4/errata
即将$(document).on "ready page:change"更改为$(document).on "ready, page:change"
https://stackoverflow.com/questions/20490435
复制相似问题