Agile Web Development with Rails, Edition 4

16 Deployment 15.3 Task J3: Translating Checkout

15.4 Task J4: Add a locale switcher.

Expected at least 1 element matching "option[value='es']", found 0.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:391

Add form for setting and showing the site based on the locale.

Use CSS to position the form.

edit public/stylesheets/depot.css
.locale {
  float: right;
  margin: -0.25em 0.1em;
}

When provided, save the locale in the session.

edit app/controllers/store_controller.rb
  def index
    if params[:set_locale]
      redirect_to store_url(:locale => params[:set_locale])
    else
      @products = Product.order(:title)
    end
  end
edit app/views/layouts/application.html.erb
  <div id="banner">
    <%= form_tag store_path, :class => 'locale' do %>
      <%= select_tag 'set_locale', 
        options_for_select(LANGUAGES, I18n.locale.to_s),
        :onchange => 'this.form.submit()' %>
      <%= submit_tag 'submit' %>
      <%= javascript_tag "$('.locale input').hide()" %>
    <% end %>
    <%= image_tag("logo.png") %>
    <%= @page_title || t('.title') %>
  </div>

Try out the form

get /en

NameError in Store#index

Showing /home/rubys/git/awdwr/edition4/work-187-30/depot/app/views/store/index.html.erb where line #6 raised:

uninitialized constant I18n::RESERVED_KEYS

Extracted source (around line #6):

3: <% end %>
4: 
5: <!-- START_HIGHLIGHT -->
6: <h1><%= t('.title_html') %></h1>
7: <!-- END_HIGHLIGHT -->
8: 
9: <% @products.each do |product| %>

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

Application Trace | Framework Trace | Full Trace
app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb__644177109_70028980718860_0'

Request

Parameters:

{"locale"=>"en"}

Show session dump

Show env dump

Response

Headers:

None

rake test
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p374/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
..........
Finished in 0.251012 seconds.
 
10 tests, 31 assertions, 0 failures, 0 errors
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p374/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
.....................E.E...............EE.......
Finished in 0.959113 seconds.
 
  1) Error:
test_should_get_edit(OrdersControllerTest):
ActionView::Template::Error: uninitialized constant I18n::RESERVED_KEYS
    app/views/orders/_form.html.erb:31:in `_app_views_orders__form_html_erb___1104391054_70197070698900_1008830'
    app/views/orders/_form.html.erb:2:in `_app_views_orders__form_html_erb___1104391054_70197070698900_1008830'
    app/views/orders/edit.html.erb:3:in `_app_views_orders_edit_html_erb__687036559_70197070707680_0'
    test/functional/orders_controller_test.rb:55:in `test_should_get_edit'
 
  2) Error:
test_should_get_new(OrdersControllerTest):
ActionView::Template::Error: uninitialized constant I18n::RESERVED_KEYS
    app/views/orders/_form.html.erb:31:in `_app_views_orders__form_html_erb___1104391054_70197070698900_1008830'
    app/views/orders/_form.html.erb:2:in `_app_views_orders__form_html_erb___1104391054_70197070698900_1008830'
    app/views/orders/new.html.erb:6:in `_app_views_orders_new_html_erb___1975375319_70197075986160_0'
    app/controllers/orders_controller.rb:41:in `new'
    test/functional/orders_controller_test.rb:32:in `test_should_get_new'
 
  3) Error:
test_markup_needed_for_store.js.coffee_is_in_place(StoreControllerTest):
ActionView::Template::Error: uninitialized constant I18n::RESERVED_KEYS
    app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb__285249087_70197074469820_0'
    test/functional/store_controller_test.rb:15:in `test_markup_needed_for_store.js.coffee_is_in_place'
 
  4) Error:
test_should_get_index(StoreControllerTest):
ActionView::Template::Error: uninitialized constant I18n::RESERVED_KEYS
    app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb__285249087_70197074469820_0'
    test/functional/store_controller_test.rb:5:in `test_should_get_index'
 
48 tests, 71 assertions, 0 failures, 4 errors
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p374/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
EEE
Finished in 0.303856 seconds.
 
  1) Error:
test_buying_a_product(DslUserStoriesTest):
ActionView::Template::Error: uninitialized constant I18n::RESERVED_KEYS
    app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb___1229304434_69960275952480_0'
    test/integration/dsl_user_stories_test.rb:39:in `test_buying_a_product'
 
  2) Error:
test_two_people_buying(DslUserStoriesTest):
ActionView::Template::Error: uninitialized constant I18n::RESERVED_KEYS
    app/views/orders/_form.html.erb:31:in `_app_views_orders__form_html_erb___1475748286_69960278986420_1014910'
    app/views/orders/_form.html.erb:2:in `_app_views_orders__form_html_erb___1475748286_69960278986420_1014910'
    app/views/orders/new.html.erb:6:in `_app_views_orders_new_html_erb___376648723_69960279095860_0'
    app/controllers/orders_controller.rb:41:in `new'
    test/integration/dsl_user_stories_test.rb:86:in `checks_out'
    test/integration/dsl_user_stories_test.rb:56:in `test_two_people_buying'
 
  3) Error:
test_buying_a_product(UserStoriesTest):
ActionView::Template::Error: uninitialized constant I18n::RESERVED_KEYS
    app/views/store/index.html.erb:6:in `_app_views_store_index_html_erb___1229304434_69960275952480_0'
    test/integration/user_stories_test.rb:19:in `test_buying_a_product'
 
3 tests, 4 assertions, 0 failures, 3 errors
Errors running test:functionals, test:integration!

16 Deployment 15.3 Task J3: Translating Checkout