【问题标题】:How do I validate restful parameters in rails at the routing level?如何在路由级别验证 Rails 中的 restful 参数?
【发布时间】:2011-05-07 14:45:17
【问题描述】:

我目前有以下restful url:

/questions/2011/05/

我的提问途径是:

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

如何在路由级别验证上述年份和月份参数,以便:

  1. 年月为整数
  2. 一年的最小/最大长度 = 4
  3. 月的最小/最大长度 = 2

在 django 中,我可以使用以下行来完成上述操作:

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

我正在浏览 Rails 指南并四处搜索,但在路由级别找不到相应的功能。以上是否意味着在控制器级别完成?

【问题讨论】:

    标签: ruby-on-rails-3 routes


    【解决方案1】:

    您正在寻找匹配方法选项哈希的约束选项。

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

    Guide | APIDock Documentation

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 2015-06-17
      • 2015-07-26
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多