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