如何访问购物车。找不到编号为'id'= 2的LineItem

时间:2019-10-23 08:53:45

标签: ruby-on-rails ruby

我正在制作一个带有Rails的电子商务网站。试图创建购物车。

添加后,

包括CurrentCart

before_action:set_cart

到我的application_controller.rb,出现此错误。 我检查了binding.pry,然后@line_item为零。

如果您能给我一个解决此问题的提示,我将不胜感激。 抱歉,长码。

错误消息

ActiveRecord::RecordNotFound at /line_items/2
Couldn't find LineItem with 'id'=2

line_items.controller

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


 def index
   @line_items = LineItem.all
 end

 def show
 end

 def new
   @line_item = LineItem.new
 end

 def edit
 end

  def create
   item = Item.find(params[:item_id])
   @line_item = @cart.add_item(item)

   respond_to do |format|
     if @line_item.save
       format.html { redirect_to @line_item.cart, notice: 'カゴに追加されました' }
       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

 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


 def destroy
   @cart = Cart.find(session[:cart_id])
   @line_item.destroy
   respond_to do |format|
     format.html { redirect_to cart_path(@cart), notice: 'Line item was successfully destroyed.' }
     format.json { head :no_content }
   end
 end

 private
   def set_line_item    
     @line_item = LineItem.find(params[:id]) #says that I have a problem here!
   end

   def line_item_params
     params.require(:line_item).permit(:item_id)
   end
end

current_cart.rb

module CurrentCart
  private
  def set_cart
    @cart = Cart.find(session[:cart_id])  
  rescue ActiveRecord::RecoedNotFound
    @cart = Cart.create
    session[:cart_id] = @cart.id
  end
end

items_controller

class ItemsController < ApplicationController
  before_action :set_item, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @items = Item.all.order("created_at desc")
  end

  def show
  end

  def new
    @item = current_user.items.build
  end

  def edit
  end


  def create
    @item = current_user.items.build(item_params)

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

  def update
    respond_to do |format|
      if @item.update(item_params)
        format.html { redirect_to @item, notice: 'Item was successfully updated.' }
        format.json { render :show, status: :ok, location: @item }
      else
        format.html { render :edit }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @item.destroy
    respond_to do |format|
      format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_item
      @item = Item.find(params[:id])
    end

    def item_params
      params.require(:item).permit(:brand, :model, :description, :condition, :finish, :title, :price, :image)
    end
end

application.controller

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include CurrentCart
  before_action :set_cart

end

路线

Rails.application.routes.draw do
  resources :line_items
  resources :carts
  resources :items
  devise_for :users, controllers:{
  registrations: 'registrations'
  }
  root 'items#index'
  devise_scope :user do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end
end

服务器日志

Started POST "/line_items/2?item_id=2" for ::1 at 2019-10-23 18:04:28 +0900
   (0.4ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
   (0.3ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC

ActionController::RoutingError (No route matches [POST] "/line_items/2"):

actionpack (6.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.0) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.0) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.0) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.0) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.0) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.0) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.0) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.0) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/method_override.rb:22:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.0) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.0) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.0) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
actionpack (6.0.0) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
webpacker (4.0.7) lib/webpacker/dev_server_proxy.rb:29:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.0) lib/rails/engine.rb:526:in `call'
puma (3.12.1) lib/puma/configuration.rb:227:in `call'
puma (3.12.1) lib/puma/server.rb:660:in `handle_request'
puma (3.12.1) lib/puma/server.rb:474:in `process_client'
puma (3.12.1) lib/puma/server.rb:334:in `block in run'
puma (3.12.1) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
Started GET "/line_items/2?item_id=2" for ::1 at 2019-10-23 18:04:52 +0900
Processing by LineItemsController#show as HTML
  Parameters: {"item_id"=>"2", "id"=>"2"}
  LineItem Load (0.4ms)  SELECT `line_items`.* FROM `line_items` WHERE `line_items`.`id` = 2 LIMIT 1
  ↳ app/controllers/line_items_controller.rb:61:in `set_line_item'
Completed 404 Not Found in 87ms (ActiveRecord: 3.5ms | Allocations: 20765)



ActiveRecord::RecordNotFound - Couldn't find LineItem with 'id'=2:
  app/controllers/line_items_controller.rb:61:in `set_line_item'

Started POST "/__better_errors/691901ca03d27e6b/variables" for ::1 at 2019-10-23 18:04:53 +0900


1 个答案:

答案 0 :(得分:0)

您有错字:

onPostExecute

应该是

rescue ActiveRecord::RecoedNotFound

更改将意味着抢救块被击中,创建卡并将其分配给您的会话。另一方面,可能只是这样做:

rescue ActiveRecord::RecordNotFound