The Depot Application

The Depot Application

12.1 Generating the XML Feed 11.3 Iteration F3: Limiting Access

11.4 Iteration F4: Adding a Sidebar, More Administration

</=&gt; #&lt;Product id: nil/> expected but was
<"class ApplicationController &lt; ActionController::Base">.

Traceback:
  /home/rubys/git/awdwr/work-188-236/vendor/rails/actionpack/lib/action_controller/assertions/selector_assertions.rb:307:in `assert_select'
  /home/rubys/git/awdwr/checkdepot.rb:181
edit app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
    layout "store"
  #...
get /admin

NoMethodError in Admin#index

Showing app/views/layouts/store.html.erb where line #24 raised:

undefined method `items' for nil:NilClass

Extracted source (around line #24):

21:     <div id="side">
22:       <!-- START_HIGHLIGHT -->
23:       <!-- START:hidden_div -->
24:       <% hidden_div_if(@cart.items.empty?, :id => 'cart') do %>
25:         <%= render(:partial => "cart", :object => @cart) %>
26:       <% end %>
27:     <!-- END:hidden_div -->

RAILS_ROOT: /home/rubys/git/awdwr/work-188-236/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Show session dump

Response

Headers:

{"Content-Type"=>"text/html",
 "Cache-Control"=>"no-cache"}

get /users

Listing users

Name
dave Show Edit Destroy

New user
edit app/views/layouts/store.html.erb
      <% if @cart %>
        <% hidden_div_if(@cart.items.empty?, :id => 'cart') do %>
          <%= render(:partial => "cart", :object => @cart) %>
        <% end %>
      <% end %>
get /admin

Welcome

It's Sun May 23 18:23:23 -0400 2010 We have 1 order.
get /users

Listing users

Name
dave Show Edit Destroy

New user
rm app/views/layouts/products.html.erb
rm app/views/layouts/users.html.erb
rm app/views/layouts/orders.html.erb
get /users

Listing users

Name
dave Show Edit Destroy

New user
edit app/models/user.rb
require 'digest/sha1'
 
class User < ActiveRecord::Base
  
  validates_presence_of     :name
  validates_uniqueness_of   :name
 
  attr_accessor :password_confirmation
  validates_confirmation_of :password
 
  validate :password_non_blank
  
  def self.authenticate(name, password)
    user = self.find_by_name(name)
    if user
      expected_password = encrypted_password(password, user.salt)
      if user.hashed_password != expected_password
        user = nil
      end
    end
    user
  end
  
  # 'password' is a virtual attribute
  def password
    @password
  end
  
  def password=(pwd)
    @password = pwd
    return if pwd.blank?
    create_new_salt
    self.hashed_password = User.encrypted_password(self.password, self.salt)
  end
  
  after_destroy :ensure_an_admin_remains
 
  def ensure_an_admin_remains
    if User.count.zero?
      raise "Can't delete last user"
    end
  end     
 
private
 
  def password_non_blank
    errors.add(:password, "Missing password") if hashed_password.blank?
  end
  
  def create_new_salt
    self.salt = self.object_id.to_s + rand.to_s
  end
  
  def self.encrypted_password(password, salt)
    string_to_hash = password + "wibble" + salt
    Digest::SHA1.hexdigest(string_to_hash)
  end
end
edit app/controllers/users_controller.rb
class UsersController < ApplicationController
  # GET /users
  # GET /users.xml
  def index
    @users = User.all(:order => :name)
 
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @users }
    end
  end
 
  # GET /users/1
  # GET /users/1.xml
  def show
    @user = User.find(params[:id])
 
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end
 
  # GET /users/new
  # GET /users/new.xml
  def new
    @user = User.new
 
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end
 
  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end
 
  # POST /users
  # POST /users.xml
  def create
    @user = User.new(params[:user])
 
    respond_to do |format|
      if @user.save
        format.html { redirect_to(users_url,
                    :notice => "User #{@user.name} was successfully created.") }
        format.xml  { render :xml => @user, :status => :created,
                             :location => @user }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @user.errors,
                             :status => :unprocessable_entity }
      end
    end
  end
 
  # PUT /users/1
  # PUT /users/1.xml
  def update
    @user = User.find(params[:id])
 
    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to(users_url,
                    :notice => "User #{@user.name} was successfully updated.") }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors,
                             :status => :unprocessable_entity }
      end
    end
  end
 
  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    begin
      @user.destroy
      flash[:notice] = "User #{@user.name} deleted"
    rescue Exception => e
      flash[:notice] = e.message
    end
 
    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end
end
edit app/controllers/store_controller.rb
      def find_cart
        @cart = (session[:cart] ||= Cart.new)
      end
edit app/controllers/store_controller.rb
      before_filter :find_cart, :except => :empty_cart
echo "Product.new" | IRBRC=tmp/irbrc ruby script/console
*** glibc detected *** irb: realloc(): invalid next size: 0x081e52e8 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb760b803]
/lib/tls/i686/cmov/libc.so.6(realloc+0x10b)[0xb760d75b]
irb(ruby_xrealloc+0x56)[0x8075696]
irb(rb_str_resize+0x53)[0x80bfcc3]
irb[0x80721c7]
irb(rb_find_file_ext+0x1d2)[0x8072a32]
irb(rb_require_safe+0x12d)[0x806726d]
irb[0x8063ab6]
irb[0x8063d81]
irb[0x805ecf3]
irb[0x8067be6]
irb[0x806806b]
irb[0x8059334]
irb[0x8063ab6]
irb[0x8063d81]
irb[0x805ecf3]
irb[0x80639fc]
irb[0x8063d81]
irb[0x805ecf3]
irb[0x805ef39]
irb[0x806b95e]
irb(rb_yield+0x21)[0x806cdc1]
irb(rb_ary_each+0x31)[0x80d1c51]
irb[0x8063ab6]
irb[0x8063d81]
irb[0x8060e11]
irb[0x80618ce]
irb[0x80639fc]
irb[0x8063d81]
irb[0x805ebc6]
irb[0x80639fc]
irb[0x8063d81]
irb[0x805ebc6]
irb[0x80639fc]
irb[0x8063d81]
irb[0x805ebc6]
irb[0x80639fc]
irb[0x8063d81]
irb[0x805ebc6]
irb[0x806d630]
irb(ruby_exec+0x16)[0x806d656]
irb(ruby_run+0x21)[0x806d9a1]
irb[0x805527f]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb75b4450]
irb[0x80551d1]
======= Memory map: ========
08048000-08102000 r-xp 00000000 08:05 20446      /home/rubys/.rvm/rubies/ruby-1.8.8-r27984/bin/ruby
08102000-08103000 rw-p 000ba000 08:05 20446      /home/rubys/.rvm/rubies/ruby-1.8.8-r27984/bin/ruby
08103000-081f8000 rw-p 08103000 00:00 0          [heap]
b7300000-b7321000 rw-p b7300000 00:00 0 
b7321000-b7400000 ---p b7321000 00:00 0 
b748a000-b74b7000 r-xp 00000000 08:05 2965573    /lib/libncurses.so.5.6
b74b7000-b74ba000 rw-p 0002c000 08:05 2965573    /lib/libncurses.so.5.6
b74ba000-b74e6000 r-xp 00000000 08:05 2965623    /lib/libreadline.so.5.2
b74e6000-b74ea000 rw-p 0002c000 08:05 2965623    /lib/libreadline.so.5.2
b74ea000-b74eb000 rw-p b74ea000 00:00 0 
b74fb000-b7586000 rw-p b74fb000 00:00 0 
b7586000-b759a000 r-xp 00000000 08:05 2998328    /lib/tls/i686/cmov/libpthread-2.7.so
b759a000-b759c000 rw-p 00013000 08:05 2998328    /lib/tls/i686/cmov/libpthread-2.7.so
b759c000-b759e000 rw-p b759c000 00:00 0 
b759e000-b76e7000 r-xp 00000000 08:05 2998314    /lib/tls/i686/cmov/libc-2.7.so
b76e7000-b76e8000 r--p 00149000 08:05 2998314    /lib/tls/i686/cmov/libc-2.7.so
b76e8000-b76ea000 rw-p 0014a000 08:05 2998314    /lib/tls/i686/cmov/libc-2.7.so
b76ea000-b76ed000 rw-p b76ea000 00:00 0 
b76ed000-b7710000 r-xp 00000000 08:05 2998318    /lib/tls/i686/cmov/libm-2.7.so
b7710000-b7712000 rw-p 00023000 08:05 2998318    /lib/tls/i686/cmov/libm-2.7.so
b7712000-b771b000 r-xp 00000000 08:05 2998316    /lib/tls/i686/cmov/libcrypt-2.7.so
b771b000-b771d000 rw-p 00008000 08:05 2998316    /lib/tls/i686/cmov/libcrypt-2.7.so
b771d000-b7745000 rw-p b771d000 00:00 0 
b7745000-b7747000 r-xp 00000000 08:05 2998317    /lib/tls/i686/cmov/libdl-2.7.so
b7747000-b7749000 rw-p 00001000 08:05 2998317    /lib/tls/i686/cmov/libdl-2.7.so
b7749000-b7750000 r-xp 00000000 08:05 2998330    /lib/tls/i686/cmov/librt-2.7.so
b7750000-b7752000 rw-p 00006000 08:05 2998330    /lib/tls/i686/cmov/librt-2.7.so
b7752000-b775c000 r-xp 00000000 08:05 2969156    /lib/libgcc_s.so.1
b775c000-b775d000 rw-p 0000a000 08:05 2969156    /lib/libgcc_s.so.1
b775d000-b7761000 r-xp 00000000 08:05 8703887    /home/rubys/.rvm/rubies/ruby-1.8.8-r27984/lib/ruby/1.8/i686-linux/readline.so
b7761000-b7762000 rw-p 00003000 08:05 8703887    /home/rubys/.rvm/rubies/ruby-1.8.8-r27984/lib/ruby/1.8/i686-linux/readline.so
b7762000-b7764000 rw-p b7762000 00:00 0 
b7764000-b7765000 r-xp b7764000 00:00 0          [vdso]
b7765000-b777f000 r-xp 00000000 08:05 1786011    /lib/ld-2.7.so
b777f000-b7781000 rw-p 00019000 08:05 1786011    /lib/ld-2.7.so
bf7ee000-bf803000 rw-p bffeb000 00:00 0          [stack]
Aborted

12.1 Generating the XML Feed 11.3 Iteration F3: Limiting Access