如何在路由级别验证Rails中的静态参数? [英] How do I validate restful parameters in rails at the routing level?

查看:80
本文介绍了如何在路由级别验证Rails中的静态参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下宁静的网址:

I currently have the following restful url:

/questions/2011/05/

我的问题途径是:

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

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

How can I validate the above year and month parameters at the route level so that:


  1. 年和月是整数

  2. 年份的最小/最大长度= 4

  3. 最小/最大月份长度= 2

在django中,我可以使用以下方法进行上述操作行:

In django, I can do the above with the following line:

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

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

I'm looking through the rails guide and googling around and I cannot find the corresponding functionality at the routing level. Is the above meant to be done at the controller level?

推荐答案

您正在寻找match方法options哈希的constraints选项

You are looking for the constraints option to the match method options hash.

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

指南 | APIDock文档

这篇关于如何在路由级别验证Rails中的静态参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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