Agile Web Development with Rails, Edition 4

11.5 Iteration F5: Making Images Clickable 11.3 Iteration F3: Highlighting Changes

11.4 Iteration F4: Hide an Empty Cart

Expected at least 1 element matching "#cart[style="display: none"]", found 0.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:215:in `block in <class:DepotTest>'

Add a blind down visual effect on the first item

edit app/views/line_items/create.js.erb
if ($('#cart tr').length == 1) { $('#cart').show('blind', 1000); }
 
$('#cart').html("<%= escape_javascript render(@cart) %>");
 
$('#current_item').css({'background-color':'#88ff88'}).
  animate({'background-color':'#114411'}, 1000);

Look at the app/helpers director

ls -p app
assets/
channels/
controllers/
helpers/
jobs/
mailers/
models/
views/
ls -p app/helpers
application_helper.rb
carts_helper.rb
line_items_helper.rb
products_helper.rb
store_helper.rb

Call a hidden_div_if helper

edit app/views/layouts/application.html.erb
      <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
        <%= render @cart %>
      <% end %>

Implement helper

edit app/helpers/application_helper.rb
module ApplicationHelper
  def hidden_div_if(condition, attributes = {}, &block)
    if condition
      attributes["style"] = "display: none"
    end
    content_tag("div", attributes, &block)
  end
end

Remove notice

edit app/controllers/carts_controller.rb
  def destroy
    @cart.destroy if @cart.id == session[:cart_id]
    session[:cart_id] = nil
    respond_to do |format|
      format.html { redirect_to store_url }
      format.json { head :no_content }
    end
  end

Demonstrate emptying and recreating the cart

get /carts/2

ActiveRecord::RecordNotFound in CartsController#show

Couldn't find Cart with 'id'=2

Extracted source (around line #78):
76
77
78
79
80
81
              
#END:setup# Use callbacks to share common setup or constraints between actions.
def set_cart
@cart = Cart.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.

Rails.root: /home/rubys/git/awdwr/edition4/work-230/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"id"=>"2"}

Response

Headers:

None
get /

Getting started

Here’s how to get rolling:

  1. Use bin/rails generate to create your models and controllers

    To see all available options, run it without parameters.

  2. Set up a root route to replace this page

    You're seeing this page because you're running in development mode and you haven't set a root route yet.

    Routes are set up in config/routes.rb.

  3. Configure your database

    If you're not using SQLite (the default), edit config/database.yml with your username and password.

11.5 Iteration F5: Making Images Clickable 11.3 Iteration F3: Highlighting Changes