Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 3.07 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Link to a shopping cart
  2. <% if current_cart.line_items.empty?%>
  3.         <%= link_to "cart", cart_path, :class =>'headertab' %>&nbsp;
  4.     <% else %>
  5.         <%= link_to "cart", cart_path(current_cart), :class =>'headertab' %>&nbsp;
  6.     <% end %>
  7.        
  8. Showing /Users/dave/rails_projects/EquiptMe/app/views/layouts/_headerexterior.html.erb where line #20 raised:
  9.  
  10. undefined local variable or method `current_cart' for #<#<Class:0x007fae841cc8e0>:0x007fae83339698>
  11. Extracted source (around line #20):
  12.  
  13. 17:         <li><%= link_to "browse gear", '/gear', :class =>'headertab' %></li>
  14. 18:         <li><%= link_to "join", '/signup', :class =>'headertab' %></li>
  15. 19:         <li>
  16. 20:         <% if current_cart.line_items.empty?%>
  17. 21:             <%= link_to "cart", cart_path, :class =>'headertab' %>&nbsp;
  18. 22:         <% else %>
  19. 23:             <%= link_to "cart", cart_path(current_cart), :class =>'headertab' %>&nbsp;
  20.        
  21. Outdoor::Application.routes.draw do
  22.   resources :line_items
  23.  
  24.   resources :carts
  25. .....
  26.        
  27. class ApplicationController < ActionController::Base
  28.   protect_from_forgery
  29.  
  30.   private
  31.  
  32.   def current_cart
  33.     Cart.find(session[:cart_id])
  34.     rescue ActiveRecord::RecordNotFound
  35.       cart = Cart.create
  36.       session[:cart_id] = cart.id
  37.       cart
  38.   end
  39. end
  40.        
  41. def show
  42.     begin
  43.     @cart = Cart.find(params[:id])
  44.     rescue ActiveRecord::RecordNotFound
  45.       logger.error "Attempt to access invalid cart #{params[:id]}"
  46.       redirect_to '/gear', notice: 'Invalid cart'
  47.     else
  48.       respond_to do |format|
  49.         format.html # show.html.erb
  50.         format.json { render json: @cart }
  51.       end
  52.    end
  53.  
  54.   def destroy
  55.     @cart = current_cart
  56.     @cart.destroy
  57.     session[:cart_id] = nil
  58.  
  59.     respond_to do |format|
  60.       format.html { redirect_to carts_url, notice: 'Your cart is currently empty' }
  61.       format.json { head :no_content }
  62.     end
  63. end
  64.  
  65.   def create
  66.     @cart = Cart.new(params[:cart])
  67.  
  68.     respond_to do |format|
  69.       if @cart.save
  70.         format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
  71.         format.json { render json: @cart, status: :created, location: @cart }
  72.       else
  73.         format.html { render action: "new" }
  74.         format.json { render json: @cart.errors, status: :unprocessable_entity }
  75.       end
  76.     end
  77.   end
  78.        
  79. def create
  80.     @cart = current_cart
  81.     gear = Gear.find(params[:gear_id])
  82.     @line_item = @cart.add_gear(gear.id)
  83.  
  84.     respond_to do |format|
  85.       if @line_item.save
  86.         format.html { redirect_to @line_item.cart }
  87.         format.json { render json: @line_item, status: :created, location: @line_item }
  88.       else
  89.         format.html { render action: "new" }
  90.         format.json { render json: @line_item.errors, status: :unprocessable_entity }
  91.       end
  92.     end
  93.   end
  94.   end
  95.        
  96. <%= link_to "my cart", cart_path(current_cart), :class =>'headertab' %>
  97.        
  98. class ApplicationController < ActionController::Base
  99.   protect_from_forgery
  100.  
  101.   private
  102.  
  103.   def current_cart
  104.     Cart.find(session[:cart_id])
  105.     rescue ActiveRecord::RecordNotFound
  106.       cart = Cart.create
  107.       session[:cart_id] = cart.id
  108.       cart
  109.   end
  110.   helper_method :current_cart
  111. end