什么是Rails中的路线

问题描述:

我想实现我的路线的充分国际化的Rails3.1应用翻译蛞蝓的最佳途径。我已经使用的Francesc解放军rails-translate-routes本地化路线的行动和资源。最后一步是能够翻译我的一些模型的slu gs。什么是Rails中的路线

路线被翻译:

http://myhost.com/products/pharmacy --> http://myhost.com/productos/farmacia 

我有形式

# routes.rb 
match 'products/:category_slug' => "products#index" 

的嵌套的路线我有一个实例#<Category id: 1, slug: "pharmacy">模型类,我做find_by_slug类别中我的ProductsController。

如何做路线的:category_slug部分翻译任何想法?

据我所知,您可以直接从您的控制器,只要你有正确I18n命名空间调用翻译帮手。

所以你的ProductsController可能包含类似以下内容:

class ProductsController < ApplicationController 
    def index 
    i18n_slug = I18n.t("locale.category.#{params[:category_slug]}") 
    @category = Category.find_by_slug(i18n_slug) 
    end 
end 

你或许应该告知自己的直接传递PARAMS到翻译引擎的潜在的安全风险,虽然我不知道任何。你也可以考虑 移动到这一个过滤器之前或到应用程序控制器是否会在多个控制器动作中使用。