Your Cart
1 | Rails, Angular, Postgres, and Bootstrap | $45.00 |
Total: | $45.00 |
---|
10.4 Playtime 10.2 Iteration E2: Handling Errors
Add empty cart button, remove flash for line item create, add totals to view.
Add button to the view.
edit app/views/carts/show.html.erb
<% if notice %>
<aside id="notice"><%= notice %></aside>
<% end %>
<h2>Your Pragmatic Cart</h2>
<ul>
<% @cart.line_items.each do |item| %>
<li><%= item.quantity %> × <%= item.product.title %></li>
<% end %>
</ul>
<%= button_to 'Empty cart', @cart, method: :delete,
data: { confirm: 'Are you sure?' } %>
Clear session and change flash notice when cart is destroyed.
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_index_url,
notice: 'Your cart is currently empty' }
format.json { head :no_content }
end
end
Try it out.
get /carts/1
post /carts/1
get http://localhost:3000/
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.
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.
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.
Remove scaffolding generated flash notice for line item create.
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 @line_item.cart }
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
Update the view to add totals.
edit app/views/carts/show.html.erb
<article>
<% if notice %>
<aside id="notice"><%= notice %></aside>
<% end %>
<h2>Your Cart</h2>
<table>
<% @cart.line_items.each do |line_item| %>
<tr>
<td class="quantity"><%= line_item.quantity %></td>
<td><%= line_item.product.title %></td>
<td class="price"><%= number_to_currency(line_item.total_price) %></td>
</tr>
<% end %>
<tfoot>
<tr>
<th colspan="2">Total:</th>
<td class="price"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</tfoot>
</table>
<%= button_to 'Empty cart', @cart,
method: :delete,
data: { confirm: 'Are you sure?' } %>
</article>
Add a method to compute the total price of a single line item.
edit app/models/line_item.rb
def total_price
product.price * quantity
end
Add a method to compute the total price of the items in the cart.
edit app/models/cart.rb
def total_price
line_items.to_a.sum { |item| item.total_price }
end
Add some style.
edit app/assets/stylesheets/carts.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 {
table {
border-collapse: collapse;
}
td {
padding: 0.5em;
}
td.quantity {
white-space: nowrap;
}
td.quantity::after {
content: " ×";
}
td.price {
font-weight: bold;
text-align: right;
}
tfoot {
th, td.price {
font-weight: bold;
padding-top: 1em;
}
th {
text-align: right;
}
td.price {
border-top: solid thin;
}
}
input[type="submit"] {
background-color: #881;
border-radius: 0.354em;
border: solid thin #441;
color: white;
font-size: 1em;
padding: 0.354em 1em;
}
input[type="submit"]:hover {
background-color: #992;
}
}
get /carts/1
get http://localhost:3000/
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.
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.
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.
get /carts/2
get http://localhost:3000/
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.
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.
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.
get /carts/3
get http://localhost:3000/
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.
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.
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.
Add a product to the cart, and see the total.
get /
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.
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.
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=2
get http://localhost:3000/carts/2
1 | Rails, Angular, Postgres, and Bootstrap | $45.00 |
Total: | $45.00 |
---|
Add a few more products, and watch the totals climb!
post /line_items?product_id=2
get http://localhost:3000/carts/2
2 | Rails, Angular, Postgres, and Bootstrap | $90.00 |
Total: | $90.00 |
---|
post /line_items?product_id=3
get http://localhost:3000/carts/2
2 | Rails, Angular, Postgres, and Bootstrap | $90.00 |
1 | Seven Mobile Apps in Seven Weeks | $26.00 |
Total: | $116.00 |
---|