English 中文(简体)
与购物车的链接
原标题:Link to a shopping cart

我创建了一个带有会话对象中存在的线条项目的购物车。 我无法理解我如何连接到购物车显示页面, 如果该手车存在的话, 只要链接到一个手车是空的页面, 如果它不存在的话。 我错过了什么? 我的代码如下:

  • Cart has_many line_items, & lineitems belongs to Cart

View Link , I wasn t sure what I should put here and where to define it for example... "if current_cart.empty? cart", or "if @cart.line_items.empty? current_cart", etc.

        <% if current_cart.line_items.empty?%>
        <%= link_to "cart", cart_path, :class => headertab  %>&nbsp;
    <% else %>
        <%= link_to "cart", cart_path(current_cart), :class => headertab  %>&nbsp;
    <% end %>

这给了我以下错误:

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

< 坚固> 电源控制器“ 显示 ”, “ 销毁” & amp; “ 创建”

 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

感谢您的帮助! < 强/ 强 >

最佳回答

您的代码中总是有一个创建的 < code> Cart 对象。 您的 < code> current_ cart 方法, 总是返回 < code> new 或先前创建的 < code> Cart 对象, 所以您必须连接到 < code> cart_ path( current_ cart)

  <%= link_to "my cart", cart_path(current_cart), :class => headertab  %>

在另一种情况下, 如果您想要检查手推车是否为空吗? 在此情况下, 您必须使用 putrent_ cart. line_ items. empty?

Edited

访问 current_cart in Views and currents 中 pelfer_method helper_method: p流_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
  helper_method :current_cart
end
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...