0

I am busy going through PBP - Agile Web Development with Rails and implementing the locale switcher.

However when I try switch between english and spanish I get a error:

No route matches [POST] "/en"

My controller is as follows:

class StoreController < ApplicationController
  skip_before_filter :authorize

  def index
    if params[:set_locale]
      redirect_to store_path(locale: params[:set_locale])
    else
      @products = Product.order(:title)
      @cart = current_cart
    end
  end
end

and an extract of the application.hmtl.erb that is being used;

<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>

the routing folder is as follows:

scope'(:locale)' do
  resources :users
  resources :orders
  resources :line_items
  resources :carts
  resources :products do
    get :who_bought, on: :member
  end
root to: 'store#index', as: 'store'
end

Cant figure out what I did wrong. If I enter /en or /es in the url it works correctly. However choosing it in the drop down that is created I get the error mentioned

Toma
  • 239
  • 3
  • 11

2 Answers2

1

Found the issue, the form_tag was expecting a POST so I changed

<%= form_tag store_path, class: 'locale' do %>

to

<%= form_tag store_path, class: 'locale', :method => :get do %>

and it worked

Toma
  • 239
  • 3
  • 11
0

You are missing an slash in the scope as guides states:

# config/routes.rb
scope "/:locale" do
  resources :books
end

http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params

Jorge de los Santos
  • 4,583
  • 1
  • 17
  • 35
  • Using the bracket just doesn't force the use of a locale in your routes, tried it anyway and still getting the same issue. Think it has something to do with the submit trying to post rather than calling as a GET but that is just a guess – Toma Jun 18 '14 at 19:27