Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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
Ruby on rails 链接到购物车_Ruby On Rails_Routes_E Commerce_Shopping Cart - Fatal编程技术网

Ruby on rails 链接到购物车

Ruby on rails 链接到购物车,ruby-on-rails,routes,e-commerce,shopping-cart,Ruby On Rails,Routes,E Commerce,Shopping Cart,我已经创建了一个购物车,其中包含会话对象中存在的行项目。我不知道如果购物车存在,我如何链接到我的购物车显示页面;如果购物车不存在,我如何链接到“购物车为空”页面。我错过了什么?我的代码如下: 购物车有许多行项目,&行项目属于购物车 查看链接 ,我不确定我应该在这里放什么,在哪里定义它,例如。。。“if current_cart.empty?cart”或“if@cart.line_items.empty?current_cart”等 应用程序控制器 class ApplicationContr

我已经创建了一个购物车,其中包含会话对象中存在的行项目。我不知道如果购物车存在,我如何链接到我的购物车显示页面;如果购物车不存在,我如何链接到“购物车为空”页面。我错过了什么?我的代码如下:

  • 购物车有许多行项目,&行项目属于购物车
查看链接 ,我不确定我应该在这里放什么,在哪里定义它,例如。。。“if current_cart.empty?cart”或“if@cart.line_items.empty?current_cart”等

应用程序控制器

class ApplicationController < ActionController::Base
  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
end
行项目控制器“创建”


谢谢你的帮助

从您的代码中,您始终拥有一个已创建的
购物车
对象。您的
current\u cart
方法将始终返回
new
或以前创建的
cart
对象,因此您必须链接到
cart\u路径(current\u cart)


从您的代码中,您总是创建了一个
Cart
对象。您的
current\u cart
方法将始终返回
new
或以前创建的
cart
对象,因此您必须链接到
cart\u路径(current\u cart)


由于某些原因,这不起作用我得到以下错误“未定义的局部变量或方法”“当前的车”“我会更新代码我想因为当前的车被标记为私有,它只在控制器内可用。由于某些原因,这不起作用我得到以下错误“未定义的局部变量或方法”“当前的车”“我会”更新代码我认为,由于当前的_车被标记为私有,因此它仅在控制器内可用。
Showing /Users/dave/rails_projects/EquiptMe/app/views/layouts/_headerexterior.html.erb where line #20 raised:

undefined local variable or method `current_cart' for #<#<Class:0x007fae841cc8e0>:0x007fae83339698>
Extracted source (around line #20):

17:         <li><%= link_to "browse gear", '/gear', :class =>'headertab' %></li>
18:         <li><%= link_to "join", '/signup', :class =>'headertab' %></li>
19:         <li>
20:         <% if current_cart.line_items.empty?%>
21:             <%= link_to "cart", cart_path, :class =>'headertab' %>&nbsp;
22:         <% else %>
23:             <%= link_to "cart", cart_path(current_cart), :class =>'headertab' %>&nbsp;
Outdoor::Application.routes.draw do
  resources :line_items

  resources :carts
.....
class ApplicationController < ActionController::Base
  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
end
 def show
    begin
    @cart = Cart.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error "Attempt to access invalid cart #{params[:id]}"
      redirect_to '/gear', notice: 'Invalid cart'
    else
      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @cart }
      end
   end

  def destroy
    @cart = current_cart
    @cart.destroy
    session[:cart_id] = nil

    respond_to do |format|
      format.html { redirect_to carts_url, notice: 'Your cart is currently empty' }
      format.json { head :no_content }
    end
end

  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
 def create
    @cart = current_cart
    gear = Gear.find(params[:gear_id])
    @line_item = @cart.add_gear(gear.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to @line_item.cart }
        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
  end
  <%= link_to "my cart", cart_path(current_cart), :class =>'headertab' %>
class ApplicationController < ActionController::Base
  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
  helper_method :current_cart
end