The Depot Application
11.4 Iteration F4: Adding a Sidebar, More Administration
11.3 Iteration F3: Limiting Access
11.4 Iteration F4: Adding a Sidebar, More Administration
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-192-235/depot
Application Trace |
Framework Trace |
Full Trace
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/whiny_nil.rb:52:in `method_missing'
/home/rubys/git/awdwr/work-192-235/depot/app/views/layouts/store.html.erb:24:in `_run_erb_app47views47layouts47store46html46erb'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/renderable.rb:34:in `block in render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:306:in `with_template'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/renderable.rb:30:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/template.rb:205:in `render_template'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:265:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:352:in `_render_with_layout'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:262:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1259:in `render_for_file'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:945:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `block in render_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `render_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1335:in `default_render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1341:in `perform_action'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:617:in `call_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:610:in `perform_action_with_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `block in perform_action_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/rescue.rb:160:in `perform_action_with_rescue'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/flash.rb:151:in `perform_action_with_flash'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:532:in `process'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:606:in `process_with_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:391:in `process'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:386:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/routing/route_set.rb:437:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/whiny_nil.rb:52:in `method_missing'
/home/rubys/git/awdwr/work-192-235/depot/app/views/layouts/store.html.erb:24:in `_run_erb_app47views47layouts47store46html46erb'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/renderable.rb:34:in `block in render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:306:in `with_template'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/renderable.rb:30:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/template.rb:205:in `render_template'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:265:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:352:in `_render_with_layout'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:262:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1259:in `render_for_file'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:945:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `block in render_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `render_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1335:in `default_render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1341:in `perform_action'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:617:in `call_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:610:in `perform_action_with_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `block in perform_action_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/rescue.rb:160:in `perform_action_with_rescue'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/flash.rb:151:in `perform_action_with_flash'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:532:in `process'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:606:in `process_with_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:391:in `process'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:386:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/routing/route_set.rb:437:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:87:in `dispatch'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:121:in `_call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:130:in `block in build_middleware_stack'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/string_coercion.rb:25:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/string_coercion.rb:25:in `call'
/home/rubys/git/rack/lib/rack/head.rb:9:in `call'
/home/rubys/git/rack/lib/rack/methodoverride.rb:24:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/params_parser.rb:15:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/session/abstract_store.rb:122:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/query_cache.rb:29:in `block in call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/query_cache.rb:9:in `cache'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/query_cache.rb:28:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/failsafe.rb:26:in `call'
/home/rubys/git/rack/lib/rack/lock.rb:11:in `block in call'
/home/rubys/git/rack/lib/rack/lock.rb:11:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:114:in `block in call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/reloader.rb:34:in `run'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:108:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/railties/lib/rails/rack/static.rb:31:in `call'
/home/rubys/git/rack/lib/rack/urlmap.rb:47:in `block in call'
/home/rubys/git/rack/lib/rack/urlmap.rb:41:in `each'
/home/rubys/git/rack/lib/rack/urlmap.rb:41:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/railties/lib/rails/rack/log_tailer.rb:17:in `call'
/home/rubys/git/rack/lib/rack/content_length.rb:13:in `call'
/home/rubys/git/rack/lib/rack/handler/webrick.rb:48:in `service'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/whiny_nil.rb:52:in `method_missing'
/home/rubys/git/awdwr/work-192-235/depot/app/views/layouts/store.html.erb:24:in `_run_erb_app47views47layouts47store46html46erb'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/renderable.rb:34:in `block in render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:306:in `with_template'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/renderable.rb:30:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/template.rb:205:in `render_template'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:265:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:352:in `_render_with_layout'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_view/base.rb:262:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1259:in `render_for_file'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:945:in `render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `block in render_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:51:in `render_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1335:in `default_render'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:1341:in `perform_action'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:617:in `call_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:610:in `perform_action_with_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `block in perform_action_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `block in ms'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activesupport/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/benchmarking.rb:68:in `perform_action_with_benchmark'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/rescue.rb:160:in `perform_action_with_rescue'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/flash.rb:151:in `perform_action_with_flash'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:532:in `process'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/filters.rb:606:in `process_with_filters'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:391:in `process'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/base.rb:386:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/routing/route_set.rb:437:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:87:in `dispatch'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:121:in `_call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:130:in `block in build_middleware_stack'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/string_coercion.rb:25:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/string_coercion.rb:25:in `call'
/home/rubys/git/rack/lib/rack/head.rb:9:in `call'
/home/rubys/git/rack/lib/rack/methodoverride.rb:24:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/params_parser.rb:15:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/session/abstract_store.rb:122:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/query_cache.rb:29:in `block in call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/query_cache.rb:9:in `cache'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/query_cache.rb:28:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/failsafe.rb:26:in `call'
/home/rubys/git/rack/lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:8:in `synchronize'
/home/rubys/git/rack/lib/rack/lock.rb:11:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:114:in `block in call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/reloader.rb:34:in `run'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:108:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/railties/lib/rails/rack/static.rb:31:in `call'
/home/rubys/git/rack/lib/rack/urlmap.rb:47:in `block in call'
/home/rubys/git/rack/lib/rack/urlmap.rb:41:in `each'
/home/rubys/git/rack/lib/rack/urlmap.rb:41:in `call'
/home/rubys/git/awdwr/work-192-235/depot/vendor/rails/railties/lib/rails/rack/log_tailer.rb:17:in `call'
/home/rubys/git/rack/lib/rack/content_length.rb:13:in `call'
/home/rubys/git/rack/lib/rack/handler/webrick.rb:48:in `service'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/rubys/.rvm/rubies/ruby-1.9.2-r26786/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Request
Parameters:
None
Show session dump
Response
Headers:
{"Cache-Control"=>"no-cache",
"Content-Type"=>"text/html"}
get /users
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
Pragmatic Bookshelf
Welcome
It's 2010-02-28 15:25:59 -0500
We have 1 order.
get /users
rm app/views/layouts/products.html.erb
rm app/views/layouts/users.html.erb
rm app/views/layouts/orders.html.erb
get /users
Pragmatic Bookshelf
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
Switch to inspect mode.
>> Product.new
=> #<Product id: nil, title: nil, description: nil, image_url: nil, created_at: nil, updated_at: nil, price: #<BigDecimal:8efb994,'0.0',4(8)>>
>>
11.4 Iteration F4: Adding a Sidebar, More Administration
11.3 Iteration F3: Limiting Access