The Depot Application

14.1 Tests Baked Right In 12.1 Generating the XML Feed

13 Task I: Internationalization

Expected at least 1 element matching "#notice", found 0.

Traceback:
  /home/rubys/git/awdwr/edition3/work-187-23/vendor/rails/actionpack/lib/action_controller/assertions/selector_assertions.rb:307:in `assert_select'
  /home/rubys/git/awdwr/edition3/checkdepot.rb:209
get /store/empty_cart

ActiveRecord::StatementInvalid in StoreController#empty_cart

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Show session dump

Response

Headers:

{"Content-Type"=>"",
 "Cache-Control"=>"no-cache"}
edit config/initializers/i18n.rb
#encoding: utf-8
I18n.default_locale = 'en'
 
LOCALES_DIRECTORY = "#{RAILS_ROOT}/config/locales/"
 
LANGUAGES = {
  'English' => 'en',
  "Espa\xc3\xb1ol" => 'es'
}

Restart the server.

edit app/views/layouts/store.html.erb
    <% form_tag '', :method => 'GET', :class => 'locale' do %>
      <%= select_tag 'locale', options_for_select(LANGUAGES, I18n.locale),
        :onchange => 'this.form.submit()' %>
      <%= submit_tag 'submit' %>
      <%= javascript_tag "$$('.locale input').each(Element.hide)" %>
    <% end %>
get /store?locale=en

ActiveRecord::StatementInvalid in StoreController#index

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"en"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>""}
edit app/controllers/application_controller.rb
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
 
class ApplicationController < ActionController::Base
  layout "store"
  #...
  before_filter :authorize, :except => :login
  before_filter :set_locale
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
 
  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
    
protected
  def authorize
    unless User.find_by_id(session[:user_id])
      flash[:notice] = "Please log in"
      redirect_to :controller => 'admin', :action => 'login'
    end
  end
 
  def set_locale
    session[:locale] = params[:locale] if params[:locale]
    I18n.locale = session[:locale] || I18n.default_locale
 
    locale_path = "#{LOCALES_DIRECTORY}#{I18n.locale}.yml"
 
    unless I18n.load_path.include? locale_path
      I18n.load_path << locale_path
      I18n.backend.send(:init_translations)
    end
 
  rescue Exception => err
    logger.error err
    flash.now[:notice] = "#{I18n.locale} translation not available"
 
    I18n.load_path -= [locale_path]
    I18n.locale = session[:locale] = I18n.default_locale
  end
end
edit public/stylesheets/depot.css
.locale {
        float:right;
        padding-top: 0.2em
}

Restart the server.

get /store?locale=es

ActiveRecord::StatementInvalid in StoreController#index

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"es"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>"text/html"}
edit app/views/layouts/store.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Pragprog Books Online Store</title>
  <%= stylesheet_link_tag "depot", :media => "all" %>
  <%= javascript_include_tag :defaults %>
</head>
<body id="store">
  <div id="banner">
    <% form_tag '', :method => 'GET', :class => 'locale' do %>
      <%= select_tag 'locale', options_for_select(LANGUAGES, I18n.locale),
        :onchange => 'this.form.submit()' %>
      <%= submit_tag 'submit' %>
      <%= javascript_tag "$$('.locale input').each(Element.hide)" %>
    <% end %>
    <%= image_tag("logo.png") %>
    <%= @page_title || I18n.t('layout.title') %>
  </div>
  <div id="columns">
    <div id="side">
      <% if @cart %>
        <% hidden_div_if(@cart.items.empty?, :id => 'cart') do %>
          <%= render(:partial => "cart", :object => @cart) %>
        <% end %>
      <% end %>
 
      <a href="http://www...."><%= I18n.t 'layout.side.home' %></a><br />
      <a href="http://www..../faq"><%= I18n.t 'layout.side.questions' %></a><br />
      <a href="http://www..../news"><%= I18n.t 'layout.side.news' %></a><br />
      <a href="http://www..../contact"><%= I18n.t 'layout.side.contact' %></a><br />
      <% if session[:user_id] %>
        <br />
        <%= link_to 'Orders',   :controller => 'orders' %><br />
        <%= link_to 'Products', :controller => 'products' %><br />
        <%= link_to 'Users',    :controller => 'users'    %><br />
        <br />
        <%= link_to 'Logout', :controller => 'admin', :action => 'logout' %>
      <% end %>
    </div>
    <div id="main">
      <% if flash[:notice] -%>
        <div id="notice"><%= flash[:notice] %></div>
      <% end -%>
 
      <%= yield :layout %>
    </div>
  </div>
</body>
</html>
edit config/locales/en.yml
en:
 
  number:
    currency:
      format:
        unit: "$"
        precision: 2
        separator: "."
        delimiter: ","
        format: "%u%n"
 
  layout:
    title:       "Pragmatic Bookshelf"
    side:
      home:      "Home"
      questions: "Questions"
      news:      "News"
      contact:   "Contact"
    cart:
      title:      "Your Cart"
      button:
        empty:    "Empty cart"
        checkout: "Checkout"
 
  main:
    title:       "Your Pragmatic Catalog"
    button:
      add:       "Add to Cart"
 
  checkout:
    legend:      "Please Enter your Details"
    name:        "Name"
    address:     "Address"
    email:       "E-mail"
    pay_type:    "Pay with"
    pay_prompt:  "Select a payment method"
    submit:      "Place Order"
 
  flash:
    thanks:      "Thank you for your order"
edit config/locales/es.yml
es:
 
  number:
    currency:
      format:
        unit: "$US"
        precision: 2
        separator: ","
        delimiter: "."
        format: "%n&nbsp;%u"
 
  activerecord:
    models:
      order: "pedido"
    attributes:
      order:
        address: "Direcci&oacute;n"
        name: "Nombre"
        email: "E-mail"
        pay_type: "Forma de pago"
    errors:
      messages:
        inclusion: "no est&aacute; incluido en la lista"
        blank:     "no puede quedar en blanco"
      template:
        body: "Hay problemas con los siguientes campos:"
        header:
          one:   "1 error ha impedido que este %{model} se guarde"
          other: "%{count} errores han impedido que este %{model} se guarde"
 
  layout:
    title:       "Publicaciones de Pragmatic"
    side:
      home:      "Inicio"
      questions: "Preguntas"
      news:      "Noticias"
      contact:   "Contacto"
    cart:
      title:      "Carrito de la Compra"
      button:
        empty:    "Vaciar Carrito"
        checkout: "Comprar"
 
  main:
    title:       "Su Cat&aacute;logo de Pragmatic"
    button:
      add:       "A&ntilde;adir al Carrito"
 
  checkout:
    legend:      "Por favor, introduzca sus datos"
    name:        "Nombre"
    address:     "Direcci&oacute;n"
    email:       "E-mail"
    pay_type:    "Pagar con"
    pay_prompt:  "Seleccione un m\xC3\xA9todo de pago"
    submit:      "Realizar Pedido"
 
  flash:
    thanks:      "Gracias por su pedido"
get /store?locale=es

ActiveRecord::StatementInvalid in StoreController#index

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"es"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>"text/html"}
edit app/views/store/index.html.erb
<h1><%= I18n.t 'main.title' %></h1>
 
<% @products.each do |product| -%>
  <div class="entry">
    <%= image_tag(product.image_url) %>
    <h3><%=h product.title %></h3>
    <%= product.description %>
    <div class="price-line">
    <span class="price"><%= number_to_currency(product.price) %></span>
    <% form_remote_tag :url=>{:action=>'add_to_cart', :id=>product} do %>
      <%= submit_tag I18n.t('main.button.add') %>
    <% end %>
    </div>
  </div>
<% end %>
get /store?locale=es

ActiveRecord::StatementInvalid in StoreController#index

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"es"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>"text/html"}
edit app/views/store/_cart.html.erb
<div class="cart-title"><%= I18n.t 'layout.cart.title' %></div>
<table>
  <%= render(:partial => "cart_item", :collection => cart.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 I18n.t('layout.cart.button.checkout'), :action => 'checkout' %>
<%= button_to I18n.t('layout.cart.button.empty'), :action => :empty_cart %>
get /store/add_to_cart/2

ActiveRecord::StatementInvalid in StoreController#add_to_cart

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"id"=>"2"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>""}
edit app/views/store/checkout.html.erb
<div class="depot-form">
  
  <%= error_messages_for 'order' %>
  
  <% form_for :order, :url => { :action => :save_order } do |form| %>
    <fieldset>
      <legend><%= I18n.t 'checkout.legend' %></legend>
 
      <div>
        <%= form.label :name, I18n.t('checkout.name') + ":" %>
        <%= form.text_field :name, :size => 40 %>
      </div>
 
      <div>
        <%= form.label :address, I18n.t('checkout.address') + ":" %>
        <%= form.text_area :address, :rows => 3, :cols => 40 %>
      </div>
 
      <div>
        <%= form.label :email, I18n.t('checkout.email') + ":" %>
        <%= form.text_field :email, :size => 40 %>
      </div>
 
      <div>
        <%= form.label :pay_type, I18n.t('checkout.pay_type') + ":" %>
        <%=
          form.select :pay_type,
                       Order::PAYMENT_TYPES, 
                      :prompt => I18n.t('checkout.pay_prompt')
        %>
      </div>
    
      <%= submit_tag I18n.t('checkout.submit'), :class => "submit" %>
    </fieldset>
  <% end %>  
</div>
edit app/controllers/store_controller.rb
class StoreController < ApplicationController
      before_filter :find_cart, :except => :empty_cart
  def index
    @products = Product.find_products_for_sale
  end
 
 
  def add_to_cart
    product = Product.find(params[:id])
    @current_item = @cart.add_product(product)
    respond_to do |format|
      format.js if request.xhr?
      format.html {redirect_to_index}
    end
  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid product #{params[:id]}")
    redirect_to_index("Invalid product")
  end
 
  def checkout
    if @cart.items.empty?
      redirect_to_index("Your cart is empty")
    else
      @order = Order.new
    end
  end
 
  def save_order
    @order = Order.new(params[:order])
    @order.add_line_items_from_cart(@cart)
    if @order.save
      session[:cart] = nil
      redirect_to_index(I18n.t('flash.thanks'))
    else
      render :action => 'checkout'
    end
  end
 
  def empty_cart
    session[:cart] = nil
    redirect_to_index
  end
 
private
 
  def redirect_to_index(msg = nil)
    flash[:notice] = msg if msg
    redirect_to :action => 'index'
  end
 
      def find_cart
        @cart = (session[:cart] ||= Cart.new)
      end
 
  #...
protected
 
  def authorize
  end
end
get /store?locale=es

ActiveRecord::StatementInvalid in StoreController#index

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"es"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>"text/html"}
get /store/add_to_cart/2

ActiveRecord::StatementInvalid in StoreController#add_to_cart

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"id"=>"2"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>""}
get /store/checkout

ActiveRecord::StatementInvalid in StoreController#checkout

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>""}
get /store/save_order

ActiveRecord::StatementInvalid in StoreController#save_order

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>""}
get /store/save_order

ActiveRecord::StatementInvalid in StoreController#save_order

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>""}
get /store?locale=en

ActiveRecord::StatementInvalid in StoreController#index

Could not find table 'sessions'

RAILS_ROOT: /home/rubys/git/awdwr/edition3/work-187-23/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"locale"=>"en"}

Show session dump

Response

Headers:

{"Cache-Control"=>"no-cache",
 "Content-Type"=>"text/html"}

14.1 Tests Baked Right In 12.1 Generating the XML Feed