簡體   English   中英

語言環境為子目錄時如何配置route.rb

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

我試圖在RoR上配置本地化設置。 即使我按照下面的指南進行了設置,也無法正常工作。 http://guides.rubyonrails.org/i18n.html

這是我的代碼。 這是簡單的餐廳清單。

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'

結果在這里。

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

您正在定義范圍之外而不是內部的非靜態路由(列表)。 如果需要堅持使用“列表”,則還應該在范圍內定義它:

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

然后轉到localhost:3000 / en / restaurants / list,您應該擁有它。

最后,我根據所有評論對其進行了修復。 最終的route.rb就是這樣。

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

我很感激

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM