Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Rails的敏捷Web开发:将两(2)项添加到depot cart,而不是一(1)项_Javascript_Ruby On Rails_Postgresql - Fatal编程技术网

Javascript 使用Rails的敏捷Web开发:将两(2)项添加到depot cart,而不是一(1)项

Javascript 使用Rails的敏捷Web开发:将两(2)项添加到depot cart,而不是一(1)项,javascript,ruby-on-rails,postgresql,Javascript,Ruby On Rails,Postgresql,因此,我使用Rails书籍完成了敏捷Web开发,并在Heroku上运行了一个功能良好的Web应用程序,该应用程序基于书中的“Depot”应用程序。我主要是编辑CSS/SASS,我做了一些事情,把我购物车中的商品数量搞砸了 我一辈子都不知道自己做了什么。现在,当我单击主页上的“添加到购物车”按钮时,两个项目被添加到我的购物车中。这和我的数据库有关吗?在photoshop/cssland中丢失了一点流之后,我完全不知所措 简言之: 我点击“添加到购物车”按钮,我点击的每一个项目都会有两个 这是我的手

因此,我使用Rails书籍完成了敏捷Web开发,并在Heroku上运行了一个功能良好的Web应用程序,该应用程序基于书中的“Depot”应用程序。我主要是编辑CSS/SASS,我做了一些事情,把我购物车中的商品数量搞砸了

我一辈子都不知道自己做了什么。现在,当我单击主页上的“添加到购物车”按钮时,两个项目被添加到我的购物车中。这和我的数据库有关吗?在photoshop/cssland中丢失了一点流之后,我完全不知所措

简言之: 我点击“添加到购物车”按钮,我点击的每一个项目都会有两个

这是我的手推车控制器:

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
class CartsController
这是我的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
class购物车
这是我的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);
$(“#注意”).hide();
if($('cart tr').length==1){$('cart').show('blind',1000);}
$('#cart').html(“”);
$('current_item').css({'background-color':'fb0f19'})。
制作动画({'background-color':'#08BBD1'},1000);
这是我的应用程序_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
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
class ApplicationController
这是我的视图/carts/_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 %>&nbsp;BTC</td>
    </tr>
</table>

<%= button_to "Checkout", new_order_path, method: :get %>
<%= button_to 'Empty cart', cart, method: :delete,
        confirm: 'Are you sure?' %>
<% end %>

你的漫画车
全部的
BTC
行项目控制器.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
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
class LineItemsController