locale参数更改其在链接中的位置

时间:2013-08-22 10:17:55

标签: ruby-on-rails routes hyperlink

我有一些严重问题,我创建了多种语言选择,效果很好,但链接一直在变化。 点击此链接时从http://localhost:3000/en/products

<%= link_to image_tag('en.png',:alt => 'description', :title =>  (I18n.t 'english') ), params.merge(:locale => :en) %>

经过一段时间后我会在应用程序中导航并点击不同的链接

http://localhost:3000/manufacturer_products?locale=en&manufacturer=Christophel

我认为问题出在routes.rb文件中。

这是我的路线文件

 root :to => 'home#index'
    devise_for :admin_users, ActiveAdmin::Devise.config
  ActiveAdmin.routes(self)
  get "about_us/index"

namespace :products do
  resources :categories do
    resources :products
  end

  resources :products, only: :index
end

namespace :products do
  resources :manufacturs do
    resources :products
  end

  resources :products, only: :index
end
  get "about/index"
  match ":locale/products/search" => "products#search"
  match "/contacts", to: "contacts#index"
  match "/products", to: "products#index"
  match "/home", to: "home#index"
  match "/about_us", to: "about_us#index"
  match "/news", to: "news#index"
  match "/manufacturer_products", to: "manufacturer_products#index"

match '/:locale' => 'home#index'
scope "(:locale)", :locale => /en|lv|ru/ do
 resources :products, :manufacturers, :categories, :news, :ActiveAdmin, :manufacturer_products

end

我明白我必须以某种方式合并命名空间:产品路由与语言环境路由,但我不知道如何开始,如果有人可以给我一个提示或smth:)

谢谢

2 个答案:

答案 0 :(得分:0)

我认为你的路线不是问题。

params.merge(:locale =&gt;:lv)具有连接2个哈希的功能,所以你应该使用像

这样的东西

params [:locale] || :EN

答案 1 :(得分:0)

我找到了解决方案,事情是在路线文件中我没有正确的路线顺序。 这条路由使用我在末尾文件中的语言环境来处理路由。所以它没有用。

scope "(:locale)", :locale => /en|lv|ru/ do
   resources :products, :manufacturers, :categories, :news, :ActiveAdmin, :manufacturer_products, :about_us, :contacts
end

我将此代码移动到文件的开头,现在它完美无缺。 :)