Agile Web Development with Rails, Edition 4
11.2 Iteration F2: Creating an AJAX-Based Cart
10.4 Playtime
11.1 Iteration F1: Moving the Cart
Refactor the cart view into partials, and reference the result from the layout.
Create a "partial" view, for just one line item
edit app/views/line_items/_line_item.html.erb
<tr>
<td><%= line_item.quantity %>×</td>
<td><%= line_item.product.title %></td>
<td class="item_price"><%= number_to_currency(line_item.total_price) %></td>
</tr>
Replace that portion of the view with a callout to the partial
edit app/views/carts/show.html.erb
<p id="notice"><%= notice %></p>
<h2>Your Cart</h2>
<table>
<%= render(@cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', @cart, method: :delete,
data: { confirm: 'Are you sure?' } %>
Make a copy as a partial for the cart controller
cp app/views/carts/show.html.erb app/views/carts/_cart.html.erb
Modify the copy to reference the (sub)partial and take input from @cart
edit app/views/carts/_cart.html.erb
<h2>Your Cart</h2>
<table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
</tr>
</table>
<%= button_to 'Empty cart', cart, method: :delete,
data: { confirm: 'Are you sure?' } %>
Keep things DRY
edit app/views/carts/show.html.erb
<p id="notice"><%= notice %></p>
<%= render @cart %>
Reference the partial from the layout.
edit app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Pragprog Books Online Store</title>
<%= csrf_meta_tags %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body class="<%= controller.controller_name %>">
<div id="banner">
<%= image_tag 'logo.svg', alt: 'The Pragmatic Bookshelf' %>
<span class="title"><%= @page_title %></span>
</div>
<div id="columns">
<div id="side">
<div id="cart">
<%= render @cart %>
</div>
<ul>
<li><a href="http://www....">Home</a></li>
<li><a href="http://www..../faq">Questions</a></li>
<li><a href="http://www..../news">News</a></li>
<li><a href="http://www..../contact">Contact</a></li>
</ul>
</div>
<div id="main">
<%= yield %>
</div>
</div>
</body>
</html>
Insert a call in the controller to find the cart
edit app/controllers/store_controller.rb
class StoreController < ApplicationController
include CurrentCart
before_action :set_cart
def index
@products = Product.order(:title)
end
end
Add a small bit of style.
edit app/assets/stylesheets/carts.css.scss
// Place all the styles related to the Carts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.carts, #side #cart {
.item_price, .total_line {
text-align: right;
}
.total_line .total_cell {
font-weight: bold;
border-top: 1px solid #595;
}
}
edit app/assets/stylesheets/application.css.scss
#side {
padding: 1em 2em;
background: #141;
form, div {
display: inline;
}
input {
font-size: small;
}
#cart {
font-size: smaller;
color: white;
table {
border-top: 1px dotted #595;
border-bottom: 1px dotted #595;
margin-bottom: 10px;
}
}
ul {
padding: 0;
li {
list-style: none;
a {
color: #bfb;
font-size: small;
}
}
}
}
Change the redirect to be back to the store.
edit app/controllers/line_items_controller.rb
def create
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product)
respond_to do |format|
if @line_item.save
format.html { redirect_to store_index_url }
format.json { render :show,
status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors,
status: :unprocessable_entity }
end
end
end
Purchase another product.
get /
Your Cart
2×
Rails, Angular, Postgres, and Bootstrap
$90.00
1×
Seven Mobile Apps in Seven Weeks
$26.00
Total
$116.00
Your Pragmatic Catalog
Rails, Angular, Postgres, and Bootstrap
Powerful, Effective, and Efficient Full-Stack Web Development
As a Rails developer, you care about user experience and performance,
but you also want simple and maintainable code. Achieve all that by
embracing the full stack of web development, from styling with
Bootstrap, building an interactive user interface with AngularJS, to
storing data quickly and reliably in PostgreSQL. Take a holistic view of
full-stack development to create usable, high-performing applications,
and learn to use these technologies effectively in a Ruby on Rails
environment.
Ruby Performance Optimization
Why Ruby Is Slow, and How to Fix It
You don’t have to accept slow Ruby or Rails performance. In this
comprehensive guide to Ruby optimization, you’ll learn how to write
faster Ruby code—but that’s just the beginning. See exactly what makes
Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you
through perils of memory and CPU optimization, profiling, measuring,
performance testing, garbage collection, and tuning. You’ll find that
all those “hard” things aren’t so difficult after all, and your code
will run orders of magnitude faster.
Seven Mobile Apps in Seven Weeks
Native Apps, Multiple Platforms
Answer the question “Can we build this for ALL the devices?” with a
resounding YES. This book will help you get there with a real-world
introduction to seven platforms, whether you’re new to mobile or an
experienced developer needing to expand your options. Plus, you’ll find
out which cross-platform solution makes the most sense for your needs.
post /line_items?product_id=3
You are being
redirected .
get http://localhost:3000/store/index
Your Cart
2×
Rails, Angular, Postgres, and Bootstrap
$90.00
2×
Seven Mobile Apps in Seven Weeks
$52.00
Total
$142.00
Your Pragmatic Catalog
Rails, Angular, Postgres, and Bootstrap
Powerful, Effective, and Efficient Full-Stack Web Development
As a Rails developer, you care about user experience and performance,
but you also want simple and maintainable code. Achieve all that by
embracing the full stack of web development, from styling with
Bootstrap, building an interactive user interface with AngularJS, to
storing data quickly and reliably in PostgreSQL. Take a holistic view of
full-stack development to create usable, high-performing applications,
and learn to use these technologies effectively in a Ruby on Rails
environment.
Ruby Performance Optimization
Why Ruby Is Slow, and How to Fix It
You don’t have to accept slow Ruby or Rails performance. In this
comprehensive guide to Ruby optimization, you’ll learn how to write
faster Ruby code—but that’s just the beginning. See exactly what makes
Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you
through perils of memory and CPU optimization, profiling, measuring,
performance testing, garbage collection, and tuning. You’ll find that
all those “hard” things aren’t so difficult after all, and your code
will run orders of magnitude faster.
Seven Mobile Apps in Seven Weeks
Native Apps, Multiple Platforms
Answer the question “Can we build this for ALL the devices?” with a
resounding YES. This book will help you get there with a real-world
introduction to seven platforms, whether you’re new to mobile or an
experienced developer needing to expand your options. Plus, you’ll find
out which cross-platform solution makes the most sense for your needs.
Run tests... oops.
rake test
Run options: --seed 20711
# Running:
..........E...F.EEEE....EEEE..
Finished in 0.279360s, 107.3882 runs/s, 225.5151 assertions/s.
1) Error:
CartsControllerTest#test_should_get_index:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/carts_controller_test.rb:9:in `block in <class:CartsControllerTest>'
2) Failure:
LineItemsControllerTest#test_should_create_line_item [/home/rubys/git/awdwr/edition4/work-226-41/depot/test/controllers/line_items_controller_test.rb:28]:
Expected response to be a redirect to <http://test.host/carts/980190963> but was a redirect to <http://test.host/store/index>.
Expected "http://test.host/carts/980190963" to be === "http://test.host/store/index".
3) Error:
LineItemsControllerTest#test_should_get_edit:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/line_items_controller_test.rb:39:in `block in <class:LineItemsControllerTest>'
4) Error:
LineItemsControllerTest#test_should_get_index:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/line_items_controller_test.rb:9:in `block in <class:LineItemsControllerTest>'
5) Error:
LineItemsControllerTest#test_should_get_new:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/line_items_controller_test.rb:15:in `block in <class:LineItemsControllerTest>'
6) Error:
LineItemsControllerTest#test_should_show_line_item:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/line_items_controller_test.rb:34:in `block in <class:LineItemsControllerTest>'
7) Error:
ProductsControllerTest#test_should_get_edit:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/products_controller_test.rb:44:in `block in <class:ProductsControllerTest>'
8) Error:
ProductsControllerTest#test_should_get_index:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/products_controller_test.rb:18:in `block in <class:ProductsControllerTest>'
9) Error:
ProductsControllerTest#test_should_get_new:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/products_controller_test.rb:24:in `block in <class:ProductsControllerTest>'
10) Error:
ProductsControllerTest#test_should_show_product:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2194255434943217322_35154280'
test/controllers/products_controller_test.rb:39:in `block in <class:ProductsControllerTest>'
30 runs, 63 assertions, 1 failures, 9 errors, 0 skips
Verify that the products page is indeed broken
get /products
HTTP Response Code: 500
ArgumentError in
Products#index
Showing /home/rubys/git/awdwr/edition4/work-226-41/depot/app/views/layouts/application.html.erb where line #25 raised:
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
Extracted source (around line #25 ):
22
23
24
25
26
27
28
<div id="side">
<!-- START_HIGHLIGHT -->
<div id="cart">
<%= render @cart %>
</div>
<!-- END_HIGHLIGHT -->
Rails.root: /home/rubys/git/awdwr/edition4/work-226-41/depot
Application Trace |
Framework Trace |
Full Trace
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2770367484130792158_28967740'
/home/rubys/git/rails/actionview/lib/action_view/renderer/partial_renderer.rb:436:in `partial_path'
/home/rubys/git/rails/actionview/lib/action_view/renderer/partial_renderer.rb:345:in `setup'
/home/rubys/git/rails/actionview/lib/action_view/renderer/partial_renderer.rb:262:in `render'
/home/rubys/git/rails/actionview/lib/action_view/renderer/renderer.rb:51:in `render_partial'
/home/rubys/git/rails/actionview/lib/action_view/helpers/rendering_helper.rb:35:in `render'
/home/rubys/git/rails/actionview/lib/action_view/template.rb:145:in `block in render'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:161:in `instrument'
/home/rubys/git/rails/actionview/lib/action_view/template.rb:339:in `instrument'
/home/rubys/git/rails/actionview/lib/action_view/template.rb:143:in `render'
/home/rubys/git/rails/actionview/lib/action_view/renderer/template_renderer.rb:67:in `render_with_layout'
/home/rubys/git/rails/actionview/lib/action_view/renderer/template_renderer.rb:53:in `render_template'
/home/rubys/git/rails/actionview/lib/action_view/renderer/template_renderer.rb:17:in `render'
/home/rubys/git/rails/actionview/lib/action_view/renderer/renderer.rb:46:in `render_template'
/home/rubys/git/rails/actionview/lib/action_view/renderer/renderer.rb:27:in `render'
/home/rubys/git/rails/actionview/lib/action_view/rendering.rb:99:in `_render_template'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/streaming.rb:217:in `_render_template'
/home/rubys/git/rails/actionview/lib/action_view/rendering.rb:82:in `render_to_body'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:32:in `render_to_body'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/renderers.rb:32:in `render_to_body'
/home/rubys/git/rails/actionpack/lib/abstract_controller/rendering.rb:25:in `render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:16:in `render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
/home/rubys/git/rails/activesupport/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
/home/rubys/git/rails/activesupport/lib/active_support/core_ext/benchmark.rb:12:in `ms'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
/home/rubys/git/rails/activerecord/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:43:in `render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/implicit_render.rb:10:in `default_render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/implicit_render.rb:5:in `send_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:189:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:10:in `process_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:113:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:113:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:552:in `block (2 levels) in compile'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:502:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:502:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:86:in `run_callbacks'
/home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:19:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rescue.rb:29:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:159:in `block in instrument'
/home/rubys/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:159:in `instrument'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
/home/rubys/git/rails/activerecord/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:136:in `process'
/home/rubys/git/rails/actionview/lib/action_view/rendering.rb:30:in `process'
/home/rubys/git/rails/actionpack/lib/action_controller/metal.rb:196:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_controller/metal.rb:232:in `block in action'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:80:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:48:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/journey/router.rb:73:in `block in call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/journey/router.rb:59:in `each'
/home/rubys/git/rails/actionpack/lib/action_dispatch/journey/router.rb:59:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:690:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/cookies.rb:562:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/query_cache.rb:36:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:380:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:82:in `run_callbacks'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/reloader.rb:73:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:38:in `call_app'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:20:in `block in call'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:68:in `block in tagged'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:26:in `tagged'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:68:in `tagged'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:20:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
/home/rubys/git/rails/railties/lib/rails/engine.rb:514:in `call'
/home/rubys/git/rails/railties/lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
/home/rubys/git/rails/actionview/lib/action_view/renderer/partial_renderer.rb:436:in `partial_path'
/home/rubys/git/rails/actionview/lib/action_view/renderer/partial_renderer.rb:345:in `setup'
/home/rubys/git/rails/actionview/lib/action_view/renderer/partial_renderer.rb:262:in `render'
/home/rubys/git/rails/actionview/lib/action_view/renderer/renderer.rb:51:in `render_partial'
/home/rubys/git/rails/actionview/lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___2770367484130792158_28967740'
/home/rubys/git/rails/actionview/lib/action_view/template.rb:145:in `block in render'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:161:in `instrument'
/home/rubys/git/rails/actionview/lib/action_view/template.rb:339:in `instrument'
/home/rubys/git/rails/actionview/lib/action_view/template.rb:143:in `render'
/home/rubys/git/rails/actionview/lib/action_view/renderer/template_renderer.rb:67:in `render_with_layout'
/home/rubys/git/rails/actionview/lib/action_view/renderer/template_renderer.rb:53:in `render_template'
/home/rubys/git/rails/actionview/lib/action_view/renderer/template_renderer.rb:17:in `render'
/home/rubys/git/rails/actionview/lib/action_view/renderer/renderer.rb:46:in `render_template'
/home/rubys/git/rails/actionview/lib/action_view/renderer/renderer.rb:27:in `render'
/home/rubys/git/rails/actionview/lib/action_view/rendering.rb:99:in `_render_template'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/streaming.rb:217:in `_render_template'
/home/rubys/git/rails/actionview/lib/action_view/rendering.rb:82:in `render_to_body'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:32:in `render_to_body'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/renderers.rb:32:in `render_to_body'
/home/rubys/git/rails/actionpack/lib/abstract_controller/rendering.rb:25:in `render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:16:in `render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
/home/rubys/git/rails/activesupport/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
/home/rubys/git/rails/activesupport/lib/active_support/core_ext/benchmark.rb:12:in `ms'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:44:in `block in render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
/home/rubys/git/rails/activerecord/lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:43:in `render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/implicit_render.rb:10:in `default_render'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/implicit_render.rb:5:in `send_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:189:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:10:in `process_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:113:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:113:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:552:in `block (2 levels) in compile'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:502:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:502:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:86:in `run_callbacks'
/home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:19:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rescue.rb:29:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:159:in `block in instrument'
/home/rubys/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:159:in `instrument'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:30:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
/home/rubys/git/rails/activerecord/lib/active_record/railties/controller_runtime.rb:18:in `process_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:136:in `process'
/home/rubys/git/rails/actionview/lib/action_view/rendering.rb:30:in `process'
/home/rubys/git/rails/actionpack/lib/action_controller/metal.rb:196:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_controller/metal.rb:232:in `block in action'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:80:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:48:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/journey/router.rb:73:in `block in call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/journey/router.rb:59:in `each'
/home/rubys/git/rails/actionpack/lib/action_dispatch/journey/router.rb:59:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:690:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/params_parser.rb:27:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/cookies.rb:562:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/query_cache.rb:36:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:380:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:82:in `run_callbacks'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/reloader.rb:73:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:38:in `call_app'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:20:in `block in call'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:68:in `block in tagged'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:26:in `tagged'
/home/rubys/git/rails/activesupport/lib/active_support/tagged_logging.rb:68:in `tagged'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:20:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
/home/rubys/git/rails/railties/lib/rails/engine.rb:514:in `call'
/home/rubys/git/rails/railties/lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/rubys/.rvm/rubies/ruby-2.2.6/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Request
Parameters :
None
_csrf_token: "eG7jW3TLb/owMXaTE91kQ80TBQlB6RswBiACvxFVd5E="
cart_id: 2
session_id: "b47cb001a22ad4e14bc8ed322184a771"
GATEWAY_INTERFACE: "CGI/1.1"
HTTP_ACCEPT: "text/html"
HTTP_ACCEPT_ENCODING: "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
REMOTE_ADDR: "127.0.0.1"
REMOTE_HOST: "127.0.0.1"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"
Response
Headers :
None
Check for nil
edit app/views/layouts/application.html.erb
<div id="side">
<div id="cart">
<% if @cart %>
<%= render @cart %>
<% end %>
</div>
<ul>
<li><a href="http://www....">Home</a></li>
<li><a href="http://www..../faq">Questions</a></li>
<li><a href="http://www..../news">News</a></li>
<li><a href="http://www..../contact">Contact</a></li>
</ul>
</div>
Update the redirect test.
edit test/controllers/line_items_controller_test.rb
test "should create line_item" do
assert_difference('LineItem.count') do
post :create, product_id: products(:ruby).id
end
assert_redirected_to store_index_path
end
all better
rake test
Run options: --seed 11533
# Running:
..............................
Finished in 0.318545s, 94.1783 runs/s, 235.4457 assertions/s.
30 runs, 75 assertions, 0 failures, 0 errors, 0 skips
11.2 Iteration F2: Creating an AJAX-Based Cart
10.4 Playtime