agile rails depot 3

appointment: this demo ran in ubuntu, and Rails 2.2.2
the demo comes from agile web development with rails 3
script>>some linux script
mysql>some db command

---------------------------------------------------------
Cart Creation --important

---------------------------------------------------------
step 1
putting sessions in the databases
script>>rake db:sessions:create
script>>rake db:migrate
mysql> desc sessions;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| session_id | varchar(255) | NO   | MUL | NULL    |                |
| data       | text         | YES  |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  | MUL | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)


step 2
use database to create session instead of the default
modify config/environment.rb
config.action_controller.session_store = :active_record_store


if not use cookie store , config app/controllers/application.rb
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery  :secret => 'efaaf2f5cc7eb43de9037be1aa8916fb'


step 3
now start to modify the code
views/store/index.html.erb
<%= button_to "Add to Cart" , :action => 'add_to_cart', :id => product %>


app/models/cart.rb
class Cart
  attr_reader :items
  def initialize
    @items = []
  end
  def add_product(product)
    @items << product
  end
end

app/controllers/store_controller.rb
class StoreController < ApplicationController
  def index
    @products = Product.find_products_for_sale
  end

  def find_cart
    session[:cart] ||= Cart.new
  end

  def add_to_cart
    
    product = Product.find(params[:id])
    logger.error( product);
    @cart = find_cart
    @cart.add_product(product)
  end

  private :find_cart

end

app/views/store/add_to_cart.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>
<% for item in @cart.items %>
<li><%=h item.title %></li>
<% end %>
</ul>


step 4
smarter cart
app/models/cart_item.rb
class CartItem  
  attr_reader :product, :quantity  
  def initialize(product)  
    @product = product  
    @quantity = 1  
  end  
  def increment_quantity  
    @quantity += 1  
  end  
  def title  
    @product.title  
  end  
  def price  
    @product.price * @quantity  
  end   
 end 


app/models/cart.rb
def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end
end

views/store/add_to_cart.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>
<% for item in @cart.items %>
<li><%= item.quantity %> &times; <%=h item.title %></li>
<% end %>
</ul>


clear sesison
script>>rake db:sessions:clear

step 5
handling Errors
def add_to_cart
  product = Product.find(params[:id])
    @cart = find_cart
    @cart.add_product(product)
  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid product #{params[:id]}" )
    flash[:notice] = "Invalid product"
    redirect_to :action => 'index'
end


display error message
views/layouts/store.html.erb
<div id="main">
<% if flash[:notice] -%>
<div id="notice"><%= flash[:notice] %></div>
<% end -%>

<%= yield :layout %>
</div>


step 5
Empty cart
views/store/add_to_cart.html.erb
<%= button_to 'Empty cart', :action => 'empty_cart' %>

app/controllers/store_controller.rb
def empty_cart
  session[:cart] = nil
  flash[:notice] = "Your cart is currently empty"
  redirect_to :action => 'index'
end

point the image to add cart
<% form_remote_tag :url => { :action => 'add_to_cart', :id => product } do %>
      <%= image_submit_tag(product.image_url)  %> 
 <% end %>

你可能感兴趣的:(mysql,ubuntu,Flash,Ruby,Rails)