首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用英语区域设置时出现奇怪的path_helper错误

使用英语区域设置时出现奇怪的path_helper错误
EN

Stack Overflow用户
提问于 2017-05-15 13:07:46
回答 2查看 164关注 0票数 2

我正在经历一个奇怪的错误,我不再想工作,而是理解。

当最近添加我们主页的英文版本时,我发现我不能像为德语地区那样生成路径:

代码语言:javascript
复制
2.3.0 :023 > I18n.available_locales
=> [:de, :en]

2.3.0 :019 > apps_path(locale: :de)
=> "/de/apps"

2.3.0 :020 > apps_path(locale: :en)
ActionController::UrlGenerationError: No route matches 
{:action=>"apps", :controller=>"pages", :locale=>:en} missing required keys: [:locale]

正如我说过的,我可以通过使用url_for或者-当坚持使用我们的示例apps_en_path来解决这个问题。

但我想知道是什么导致了这个奇怪的问题。

编辑

git bisect会话帮助我发现,自从我将路线-中转宝石添加到我的项目后,就会发生这种情况。除了这个问题,对我们来说也很好。

我有这样的设置(在development.rb中):

代码语言:javascript
复制
# RouteTranslator.config
config.force_locale = true
config.locale_param_key = :locale

我的routes.rb

代码语言:javascript
复制
require_relative "#{Rails.root}/app/route_constraints/can_access_devops"

Rails.application.routes.draw do
  get 'health', to: 'health#show'

  # Redirect requests with trailing 'null'
  get '/*path/null', to: redirect('/%{path}')

  namespace :admin do
    root to: redirect('/admin/magazine_articles')

    resources :jobs
    resources :users
    resources :magazine_categories
    resources :magazine_articles
    resources :media_assets
    resources :advertisements do
      member do
        get 'preview'
      end
    end

    namespace :devops do
      authenticate :user do
        constraints CanAccessDevOps do
          mount PgHero::Engine, at: '/pghero'
        end
      end
    end
  end

  scope '/:locale', locale: /#{I18n.available_locales.join("|")}/ do
    root to: 'pages#home'

    localized do
      devise_for :users, controllers: { sessions: 'sessions' }

      # redirect terms of use to cockpit; this is required because
      # the packaging has a link that should point to cockpit
      get 'home',        to: 'pages#home'
      get 'imprint',     to: 'pages#imprint', as: 'imprint'
      get 'lottery',     to: 'pages#lottery', as: 'lottery'
      get 'privacy',     to: 'pages#privacy', as: 'privacy'
      get 'terms',       to: 'pages#terms',   as: 'terms'
      get 'faq',         to: 'pages#faq',     as: 'faq'
      get 'declaration-of-conformity', to: 'pages#declaration_of_conformity', as: 'declaration_of_conformity'

      # Contact Page / Support Cases
      resource :support_cases, only: [:create, :new], path_names: { new: 'new_support_case' } do
        get :success, on: :collection
      end

      # redirect using the url helper to respect route localization. ugly but it works
      get 'kontakt', to: redirect { |_, _| Rails.application.routes.url_helpers.new_support_cases_path }

      # Workshop
      get 'workshop', to: 'pages#workshop', as: 'workshop'
      get 'autofit',  to: 'pages#autofit',  as: 'autofit'

      # App-related
      get 'app',     to: 'pages#app',     as: 'app'
      get 'apps',    to: 'pages#apps',    as: 'apps'
      get 'cars',    to: 'pages#cars',    as: 'cars'
      get 'roadmap', to: 'pages#roadmap', as: 'roadmap'

      # Press Material
      get 'press',            to: 'press_materials#index',      as: 'press_materials'
      get 'press/brand',      to: 'press_materials#brand',      as: 'press_materials_brand'
      get 'press/team',       to: 'press_materials#team',       as: 'press_materials_team'
      get 'press/app',        to: 'press_materials#app',        as: 'press_materials_app'
      get 'press/pace-link',  to: 'press_materials#pace_link',  as: 'press_materials_pace_link'
      get 'press/graphics',   to: 'press_materials#graphics',   as: 'press_materials_graphics'

      # Feature pages
      get 'features/automatic-emergency-call',  to: 'features#ecall',                as: 'ecall'
      get 'features/fuel-saving-trainer',       to: 'features#fuel_saving_trainer',  as: 'fuel_saving_trainer'
      get 'features/trouble-code-analysis',     to: 'features#trouble_code_analysis', as: 'trouble_code_analysis'
      get 'features/find-my-car',               to: 'features#find_my_car',          as: 'find_my_car'
      get 'features/logbook',                   to: 'features#logbook',              as: 'logbook'
      # old route – preserved as there might be old links somewhere pointing at this
      get 'features/automatisches-fahrtenbuch', to: 'features#automatic_logbook'
      # actual route: find-the-cheapest-gas-station
      get 'features/gas-station-finder',        to: 'features#gas_station_finder',   as: 'gas_station_finder'
      get 'features/fuel-cost-tracking',        to: 'features#fuel_cost_tracking',   as: 'fuel_cost_tracking'
      get 'features/performance-monitor',       to: 'features#performance_monitor',  as: 'performance_monitor'
      get 'features/traffic-monitor',           to: 'features#traffic_monitor',      as: 'traffic_monitor'

      # Endpoints for car finder
      get 'cars/query', to: 'cars#identify'
      get 'cars/:id',   to: 'cars#show'

      # Job adverts
      get 'jobs',        to: 'jobs#index',    as: 'jobs'
      get 'jobs/:slug',  to: 'jobs#show',     as: 'job'

      # Newsletter related routes
      post 'newsletter-sign-up',              to: 'subscribers#newsletter_create', as: 'newsletter'
      get 'newsletter-confirmation(/:list)',  to: 'subscribers#newsletter_after_confirm'
    end

    # Routes that we want to have under the german locale, but without translated route go here:

    # Magazine
    scope 'magazin' do
      get '', to: 'magazine#index', as: 'magazine', constraints: { locale: 'de' }

  # Redirect path without locale to locale equivalent path
  get '/*path', to: redirect("/#{I18n.locale}/%{path}"),
                constraints: lambda { |req|
                  !req.path.match(%r{^\/(404|422|500)$}) &&
                    !req.path.match(%r{^\/(#{I18n.available_locales.join("|")})\/.*})
                }

  # Redirect root to detected locale
  root to: 'application#redirect_to_localized_root', as: :redirected_root
end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-15 17:00:41

我很快就完成了你的路线,我也有类似的问题。我认为您获得的apps_path路径只允许默认的区域设置为param。

添加以下选项为我解决了这个问题:

代码语言:javascript
复制
RouteTranslator.config do |config|
  config.generate_unlocalized_routes = true
end

这将正确地生成未本地化的路由,因此可以将多个区域设置传递给apps_path。

票数 2
EN

Stack Overflow用户

发布于 2017-05-15 15:33:18

在我看来,除了这个localized块之外,没有什么东西是超级可疑的。这是什么宝石吗?如果是这样的话,apps_path(locale: 'en')请求在这个localized块之外工作吗?

在调整路由文件之后,可以在控制台上使用Rails.application.routes.url_helpers.app_path(locale: :en)进行测试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43980404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档