Noriaki Takamizawa
Noriaki Takamizawa

Reputation: 1049

How to config route.rb when locale is sub-directory

I tried to configure localize setting on RoR. Even though I set up along with guide below, it doesn't work correctly. http://guides.rubyonrails.org/i18n.html

Here's my code. Here's simple restaurant list.

application_controller.rb

  before_action :set_locale

  def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
  end

  def default_url_options(options = {})
    { locale: I18n.locale }.merge options
  end

routes.rb

 # Locale Information
scope "(:locale)" do
  resources :restaurants
end

  # Example of regular route:
get 'restaurant/list' => 'restaurant#list'
get 'hello/index' => 'hello#index'

And results are here.

Routing Error
No route matches [GET] "/en/restaurant/list"

Upvotes: 0

Views: 96

Answers (2)

Noriaki Takamizawa
Noriaki Takamizawa

Reputation: 1049

Finally, I fixed it following all the comments. The final route.rb is like this.

scope "(:locale)" do
   get 'restaurant/list' => 'restaurant#list'
end

I appreciate all.

Upvotes: 0

Marcelo Ribeiro
Marcelo Ribeiro

Reputation: 1738

You are defining a non restful route (list) outside your scope, but not inside. If you need to stick to 'list', you should define it inside your scope as well:

# Locale Information
scope "(:locale)" do
 resources :restaurants do 
  get :list, on: :collection
 end
end

Then go to localhost:3000/en/restaurants/list and you should have it.

Upvotes: 1

Related Questions