不能在购物车中增加相同的产品

时间:2017-11-01 16:03:49

标签: ruby-on-rails ruby ruby-on-rails-5

我似乎无法添加一种方法来增加我的应用程序中的相同产品。我正在设计一个在线商店,我希望能够将产品添加到购物车,以便在相同的项目中的数量推车会增加, 当我运行该程序它开始没问题,但当我将产品添加到购物车时,它给了我这个错误......

ruby 2.4.1p111 (2017-03-22 revision 58053) [i386-mingw32]

C:\Users\COMPUTER>cd desktop

C:\Users\COMPUTER\Desktop>cd trial

C:\Users\COMPUTER\Desktop\trial>cd depot

C:\Users\COMPUTER\Desktop\trial\depot>rails s
=> Booting Puma
=> Rails 5.0.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.10.0 (ruby 2.4.1-p111), codename: Russell's Teapot
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
Started POST "/line_items?product_id=3" for 127.0.0.1 at 2017-11-01 19:32:19 +0100
  ActiveRecord::SchemaMigration Load (1.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by LineItemsController#create as HTML
  Parameters: {"authenticity_token"=>"i1T0s2RDpZMJfxGvhgygNCSXXowIblsp4R+t1rrIOe9NKPkB83qppszYRtmW65uCUPOJyUPPq9p3yVz+pCCvcw==", "product_id"=>"3"}
  Cart Load (1.0ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ?  [["id", 17], ["LIMIT", 1]]
  Product Load (1.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
Completed 500 Internal Server Error in 1005ms (ActiveRecord: 14.0ms)



NoMethodError (private method `add_product' called for #<Cart:0x4de4bb0>):

app/controllers/line_items_controller.rb:29:in `create'
  Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (223.0ms)
  Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (44.0ms)
  Rendering C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (7.0ms)
  Rendered C:/Ruby24/lib/ruby/gems/2.4.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (11138.2ms)

这是代码所在的控制器.......

class LineItemsController < ApplicationController
  include CurrentCart
  before_action :set_cart, only: [:create]
  before_action :set_line_item, only: [:show, :edit, :update, :destroy]

  # GET /line_items
  # GET /line_items.json
  def index
    @line_items = LineItem.all
  end
  # GET /line_items/1
  # GET /line_items/1.json
  def show
  end

  # GET /line_items/new
  def new
    @line_item = LineItem.new
  end

  # GET /line_items/1/edit
  def edit
  end

  # POST /line_items
  # POST /line_items.json
  def create
    product = Product.find(params[:product_id])  
    @line_item = @cart.add_product(product)

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

  # PATCH/PUT /line_items/1
  # PATCH/PUT /line_items/1.json
  def update
    respond_to do |format|
      if @line_item.update(line_item_params)
        format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
        format.json { render :show, status: :ok, location: @line_item }
      else
        format.html { render :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.destroy
    respond_to do |format|
      format.html { redirect_to line_items_url, notice: 'Line item was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def line_item_params
      params.require(:line_item).permit(:product_id, :cart_id)
    end
end

这是cart.rb .......

class Cart < ApplicationRecord
    has_many :line_items, dependent: :destroy
end

def add_product(product)
 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 

这是索引

<p id="notice"><%= notice %></p> 

<h1>Your Pragmatic Catalog</h1> 
<% cache @products do %>
<% @products.each do |product| %> 
<% cache @product do %>
<div class="entry"> 


<h3><%= product.title %></h3> 

<%= image_tag(product.image_url) %>
<%= sanitize(product.description) %>
<div class="price_line"> 

<span class="price"><%= number_to_currency (product.price) %></span> 

<%= button_to 'Add to Cart' , line_items_path(product_id: product.id)%>
</div> 
</div> 
<% end %>
<% end %>
<% end %> 

0 个答案:

没有答案