Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

9.4 Playtime 9.2 Iteration D2: Connecting Products to Carts

9.3 Iteration D3: Adding a button

Now we connect the model objects we created to the controller and the view.

Add the button, connecting it to the Line Item Controller, passing the product id.

edit app/views/store/index.html.erb
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
 
<h1>Your Pragmatic Catalog</h1>
 
<% @products.each do |product| %>
  <div class="entry">
    <%= image_tag(product.image_url) %>
    <h3><%= product.title %></h3>
    <%=raw product.description %>
    <div class="price_line">
      <span class="price"><%= number_to_currency(product.price) %></span>
      <%= button_to 'Add to Cart', line_items_path(:product_id => product) %>
    </div>
  </div>
<% end %>

Add a bit of style to make it show all on one line

edit public/stylesheets/depot.css
#store .entry form, #store .entry form div {
  display: inline;
}

Update the LineItem.new call to use find_or_create_cart and the product id. Additionally change the logic so that redirection upon success goes to the cart instead of the line item.

edit app/controllers/line_items_controller.rb
  def create
    @cart = find_or_create_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.line_items.build(:product => product)
 
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to(@line_item.cart,
          :notice => 'Line item was successfully created.') }
        format.xml  { render :xml => @line_item,
          :status => :created, :location => @line_item }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @line_item.errors,
          :status => :unprocessable_entity }
      end
    end
  end

Try it once, and see that the output isn't very useful yet.

get /

Your Pragmatic Catalog

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

$34.95
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.50
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

$42.95
post /line_items?product_id=3
You are being redirected.
get http://localhost:3000/carts/1

Line item was successfully created.

Edit | Back

Update the template that shows the Cart.

edit app/views/carts/show.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>    
  <% for item in @cart.line_items %>
    <li><%= item.product.title %></li>
  <% end %>
</ul>

Try it once again, and see that the products in the cart.

get /

Your Pragmatic Catalog

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

$34.95
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.50
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

$42.95
post /line_items?product_id=3
You are being redirected.
get http://localhost:3000/carts/1

Your Pragmatic Cart

  • Programming Ruby 1.9
  • Programming Ruby 1.9
pub depot_f

9.4 Playtime 9.2 Iteration D2: Connecting Products to Carts