2011-05-07 84 views
4

我現在有以下寧靜網址:如何在路由級別驗證rails中的restful參數?

/questions/2011/05/ 

我對問題的路線是:

match 'questions/:year/:month/' => 'Questions#month' 

我怎麼可以在路線水平,使驗證上述年份和月份參數:

  1. 年和月是整數
  2. 最小/最大年份長度= 4
  3. 月的
  4. 最小/最大長度= 2

在Django,我可以做上面與下面的行:

url(r'^questions/(?P<year>\d{4})/(?P<month>\d{2})/$', 'questions.views.month'), 

我期待通過導軌引導和谷歌搜索周圍,我不能在路由級別找到相應的功能。上述是否意味着在控制器層面完成?

回答

3

您正在尋找匹配方法選項散列的約束選項。

match 'questions/:year/:month/' => 'questions#month', :constraints => {:year => /\d{4}/, :month => /\d{2}/} 

Guide | APIDock Documentation