创建带有可选路径前缀的路由 [英] Creating routes with an optional path prefix

查看:117
本文介绍了创建带有可选路径前缀的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使我的路由识别如下可选的前缀参数:

How can I go about making my routes recognise an optional prefix parameter as follows:

/*lang/controller/id

其中lang部分是可选的,并且如果未在URL中指定,则具有默认值:

In that the lang part is optional, and has a default value if it's not specified in the URL:

/en/posts/1   => lang = en
/fr/posts/1   => lang = fr
/posts/1      => lang = en

EDIT

理想情况下,我希望通过映射名称空间在许多控制器和操作上做到这一点:

Ideally, I'm looking to do this across many controllers and actions by mapping a namespace:

map.namespace "*lang" do |lang|
  lang.resources :posts
  lang.resources :stories
end


推荐答案

好的,我设法解决了这个问题:

OK, I've managed to sort out this problem:

默认情况下,在Rails中无法做到这一点(至少现在还没有)。除了使用名称空间和默认值,我还需要安装 Sven Fuchs的路由过滤器

THere is no way of doing this in Rails by default (at least, not yet). Instead of using namespaces and default values, I needed to install Sven Fuchs' routing filter.

安装插件后,我将以下文件添加到我的lib目录中:

Once the plugin is installed, I added the following file to my lib directory:

require 'routing_filter/base'

module RoutingFilter
  class Locale < Base

    # remove the locale from the beginning of the path, pass the path
    # to the given block and set it to the resulting params hash
    def around_recognize(path, env, &block)
      locale = nil
      path.sub! %r(^/([a-zA-Z]{2})(?=/|$)) do locale = $1; '' end
      returning yield do |params|
        params[:locale] = locale || 'en'
      end
    end

    def around_generate(*args, &block)
      locale = args.extract_options!.delete(:locale) || 'en'
      returning yield do |result|
        if locale != 'en'
          result.sub!(%r(^(http.?://[^/]*)?(.*))){ "#{$1}/#{locale}#{$2}" }
        end 
      end
    end

  end
end

我将此行添加到routes.rb:

I added this line to routes.rb:

map.filter 'locale'

这基本上是由插件,用于包装Rails路由。

This basically fills out a before and after hook, generated by the plugin, that wraps the rails routing.

当识别出URL且在Rails对其进行任何处理之前,将调用around_recognize方法。

When a url is recognised, and before Rails gets to do anything with it, the around_recognize method is called. This will extract a two-letter code representing the locale, and pass it through in the params, defaulting to 'en' if no locale is specified.

类似地,当a被选中时,将提取一个代表该语言环境的两个字母的代码,并将其传递给参数,如果未指定语言环境,则默认为'en'。生成了url,locale参数将被推入左侧的URL。

Likewise, when a url is generated, the locale parameter will be pushed into the URL on the left side.

这为我提供了以下url和映射:

This gives me the following urls and mappings:

/   => :locale => 'en'
/en => :locale => 'en'
/fr => :locale => 'fr'

所有现有的url辅助程序都像以前一样工作,唯一的区别是除非语言环境为指定,则将其保留:

All existing url helpers work as before, with the only difference being that unless the locale is specified, it is preserved:

home_path                  => /
home_path(:locale => 'en') => /
home_path(:locale => 'fr') => /fr

这篇关于创建带有可选路径前缀的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆