簡體   English   中英

無法添加到購物車導軌5

[英]cant add to cart rails 5

我的rails版本是5.0.5,我目前正在開發一個在線商店。 我正在遵循敏捷Web開發的步驟(第5條)。 我已經按照相應的步驟進行操作,可以創建新產品,編輯產品和刪除產品,當我單擊“添加”時,我正處於將產品添加到購物車的階段(顯示“添加到購物車”按鈕)放入購物車”,這給了我一個錯誤{無法找到ID為ID的產品}。.........這是錯誤的人。

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 GET "/" for 127.0.0.1 at 2017-10-22 17:44:40 +0100
  ActiveRecord::SchemaMigration Load (0.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by StoreController#index as HTML
  Rendering store/index.html.erb within layouts/application
  Product Load (4.0ms)  SELECT "products".* FROM "products" ORDER BY "products"."title" ASC
  Rendered store/index.html.erb within layouts/application (620.0ms)
Completed 200 OK in 1888ms (Views: 1385.1ms | ActiveRecord: 20.0ms)


Started POST "/line_items" for 127.0.0.1 at 2017-10-22 17:46:14 +0100
Processing by LineItemsController#create as HTML
  Parameters: {"authenticity_token"=>"tysgjA3w3dfMA1ACBRWeigDDnDM9WDiwBDKotUwmXVVxVy0+msnR4gmkB3QV8qU8dKdLdnb5yEOS5FmdUs7LyQ=="}
  Cart Load (4.0ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ?  [["id", nil], ["LIMIT", 1]]
   (4.0ms)  begin transaction
  SQL (48.0ms)  INSERT INTO "carts" ("created_at", "updated_at") VALUES (?, ?)  [["created_at", "2017-10-22 16:46:15.531901"], ["updated_at", "2017-10-22 16:46:15.531901"]]
   (108.0ms)  commit transaction
  Product Load (4.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT ?  [["id", nil], ["LIMIT", 1]]
Completed 404 Not Found in 644ms (ActiveRecord: 180.0ms)



ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=):

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 (52.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 (24.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 (16.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 (8928.0ms)

下面也是商店(顯示產品的默認主頁)視圖文件.....

<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   %>
</div> 
</div> 
<% end %>
<% end %>
<% end %>

以下是路線...

Rails.application.routes.draw do
  resources :line_items
  resources :carts
  root 'store#index', as: 'store_index'

  resources :products
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

下面是產品型號……

class Product < ApplicationRecord 

        validates :title, :description, :image_url, presence: true 
        validates :price, numericality: {greater_than_or_equal_to: 0.01} 
        validates :title, uniqueness: true 
        validates :image_url, allow_blank: true, format: { 
            with: %r{\.(gif|jpg|png)\Z}i, 
            message: 'must be a URL for GIF, JPG or PNG image.' 
            } 




        has_many :line_items
        before_destroy :ensure_not_referenced_by_any_line_item




  #...
    private
     # ensure that there are no line items referencing this product
          def ensure_not_referenced_by_any_line_item
            unless line_items.empty?
             errors.add(:base, 'Line Items present')
                throw :abort
          end
    end
end

下面是購物車模型...

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

def add_product (lineitem, 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

訂單項模型...

class LineItem < ApplicationRecord
  belongs_to :product
  belongs_to :cart
end

application_record.rb ........

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end

推車控制器...

class CartsController < ApplicationController
  before_action :set_cart, only: [:show, :edit, :update, :destroy]

  # GET /carts
  # GET /carts.json
  def index
    @carts = Cart.all
  end

  # GET /carts/1
  # GET /carts/1.json
  def show
  end

  # GET /carts/new
  def new
    @cart = Cart.new
  end

  # GET /carts/1/edit
  def edit
  end

  # POST /carts
  # POST /carts.json
  def create
    @cart = Cart.new(cart_params)

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

  # PATCH/PUT /carts/1
  # PATCH/PUT /carts/1.json
  def update
    respond_to do |format|
      if @cart.update(cart_params)
        format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
        format.json { render :show, status: :ok, location: @cart }
      else
        format.html { render :edit }
        format.json { render json: @cart.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /carts/1
  # DELETE /carts/1.json
  def destroy
    @cart.destroy
    respond_to do |format|
      format.html { redirect_to carts_url, notice: 'Cart was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def cart_params
      params.fetch(:cart, {})
    end
end

application controller.....
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
end

訂單項控制器...

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.line_items.build(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

產品總監...

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

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

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:title, :description, :image_url, :price)
    end
end

商店控制器。

class StoreController < ApplicationController
  def index
    @products = Product.order(:title)

  end
end

您的錯誤是ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=): 這意味着控制器不知道要添加到購物車的產品。 這可以通過將product_id添加到POST的參數中來完成。

您認為:

<%= button_to 'Add to Cart' , line_items_path   %>

line_items_path可能不正確。 您可能需要line_item_path(product_id: product.id) 這會將產品的ID添加到請求中,並在控制器嘗試在此處查找記錄時使其可用:

product = Product.find (params[:product_id])

在視圖中,您必須按照Daniel Westendorf的建議發送正確的product_id

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

因此,以這種方式,當您在LinesItemController new方法中創建新的LineItem時,就可以使用它

def new
  @line_item = LineItem.new(params[:product_id])
end

要使用調試模式,可以使用byebug ,也可以使用此step_by_step安裝指南。 是另一種選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM