Rails 4 / i18n / 无法将区域设置传递到另一个页面

Rails 4 / i18n / can't pass the locales to another page

我正在制作一个带有主页和联系表的简单网站。现在我正在尝试使用 rails-i18n gem 将它变成 3 种语言,并且在主页上成功了,但现在我被困在如何从主页传递语言环境页面到联系页面。代码如下,有什么线索请告诉我。

routes.rb(删除范围内的2个资源没有区别)

Rails.application.routes.draw do

  get 'contact' => 'inquiry#contact'
  post 'contact/confirm' => 'inquiry#confirm'
  post 'contact/thanks' => 'inquiry#thanks'

  root 'static_pages#home'
  match '/home', to: 'static_pages#home', via: 'get'

  scope '(:locale)', locale: /#{I18n.available_locales.map(&:to_s).join('|')}/ do
    resources :static_pages
    resources :inquiry
  end

end

application_controller.rb(删除default_url_options没有区别)

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :set_locale

  def set_locale
    I18n.locale = locale
  end

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

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

inquiry_controller.rb(调用联系人时,语言环境变为默认的 :en)

class InquiryController < ApplicationController
  def contact
    @inquiry = Inquiry.new
    render :action => 'contact'
  end

  def confirm
    @inquiry = Inquiry.new(params[:inquiry])
    if @inquiry.valid?
      render :action => 'confirm'
    else
      render :action => 'contact'
    end
  end

  def thanks
    @inquiry = Inquiry.new(params[:inquiry])
    InquiryMailer.received_email(@inquiry).deliver

    render :action => 'thanks'
  end
end

_dropdown.html.erb(更改语言的下拉菜单是我在首页添加的,联系方式页面没有添加)

<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Languages <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <li><%= link_to '英語/English', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'en') %></li>
    <li><%= link_to '中文/Mandarin', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'ma') %></li>
    <li><%= link_to '日本語/Japanese', url_for(controller: controller.controller_name, action: controller.action_name, locale: 'ja') %></li>
  </ul>
</li>

console(感觉哪里不对...)

$ rake routes
          Prefix Verb   URI Pattern                                Controller#Action
         contact GET    /contact(.:format)                         inquiry#contact
 contact_confirm POST   /contact/confirm(.:format)                 inquiry#confirm
  contact_thanks POST   /contact/thanks(.:format)                  inquiry#thanks
            root GET    /                                          static_pages#home
            home GET    /home(.:format)                            static_pages#home
    static_pages GET    (/:locale)/static_pages(.:format)          static_pages#index {:locale=>/en|ma|ja/}
                 POST   (/:locale)/static_pages(.:format)          static_pages#create {:locale=>/en|ma|ja/}
 new_static_page GET    (/:locale)/static_pages/new(.:format)      static_pages#new {:locale=>/en|ma|ja/}
edit_static_page GET    (/:locale)/static_pages/:id/edit(.:format) static_pages#edit {:locale=>/en|ma|ja/}
     static_page GET    (/:locale)/static_pages/:id(.:format)      static_pages#show {:locale=>/en|ma|ja/}
                 PATCH  (/:locale)/static_pages/:id(.:format)      static_pages#update {:locale=>/en|ma|ja/}
                 PUT    (/:locale)/static_pages/:id(.:format)      static_pages#update {:locale=>/en|ma|ja/}
                 DELETE (/:locale)/static_pages/:id(.:format)      static_pages#destroy {:locale=>/en|ma|ja/}
   inquiry_index GET    (/:locale)/inquiry(.:format)               inquiry#index {:locale=>/en|ma|ja/}
                 POST   (/:locale)/inquiry(.:format)               inquiry#create {:locale=>/en|ma|ja/}
     new_inquiry GET    (/:locale)/inquiry/new(.:format)           inquiry#new {:locale=>/en|ma|ja/}
    edit_inquiry GET    (/:locale)/inquiry/:id/edit(.:format)      inquiry#edit {:locale=>/en|ma|ja/}
         inquiry GET    (/:locale)/inquiry/:id(.:format)           inquiry#show {:locale=>/en|ma|ja/}
                 PATCH  (/:locale)/inquiry/:id(.:format)           inquiry#update {:locale=>/en|ma|ja/}
                 PUT    (/:locale)/inquiry/:id(.:format)           inquiry#update {:locale=>/en|ma|ja/}
                 DELETE (/:locale)/inquiry/:id(.:format)           inquiry#destroy {:locale=>/en|ma|ja/}

根据我提到的设置所有这些的博客页面,routes.rb 中的范围应该允许通过 /en 或 /ja 等访问,它会导致路由错误。我觉得我没有把整件事都做对...任何小建议将不胜感激!

通过将 link 更改为 /contact 从 href="/contact" 到 <%= link_to t('view.contact'), url_for(controller: :inquiry, action: :contact) %>