How do I validate restful parameters in rails at the routing level?

早过忘川 提交于 2019-12-12 12:12:15

问题


I currently have the following restful url:

/questions/2011/05/

My route for questions is:

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

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

  1. year and month are integers
  2. min/max length of year = 4
  3. min/max length of month = 2

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'),

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?


回答1:


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}/}

Guide | APIDock Documentation



来源:https://stackoverflow.com/questions/5921771/how-do-i-validate-restful-parameters-in-rails-at-the-routing-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!