I'm fairly new to ruby api development and have created the below endpoint in my routes.rb
我对ruby api开发相当新,并在我的routes.rb中创建了以下端点
get "/users/active_users/:since"
However, when the param is not given, I want the param to default to a certain value. How do I enforce this? Also, I want to enforce that param be an integer and not alpha/alpha-numeric. Help is appreciated!
但是,当没有给出参数时,我希望param默认为某个值。我该如何执行?此外,我想强制该param是一个整数而不是alpha / alpha-numeric。感谢帮助!
1 个解决方案
#1
1
get "/users/active_users/:since"
By making this routes its compulsory to give the params[:since]
, other wise it will throw back a error not routes matches
通过使这条路线成为强制给出params [:since],其他方面它会抛出错误而不是路线匹配
So here by i would suggest you to make routes
所以我建议你做路线
get "/users/active_users"
Since it's get
type request so its won't affect more, you can append params[:since]
in query with routes like this:-
因为它是获取类型请求所以它不会影响更多,你可以在查询中添加params [:since]与这样的路由: -
/users/active_users?since=1999
And at your controller you will get params[:since] = 1999
在你的控制器你会得到params [:since] = 1999
So far as i know it can't be enforce routes to accept only integer params
but it can be handle at controller side
据我所知,它不能强制路由只接受整数参数,但它可以在控制器端处理
params[:since].is_a? Integer
=> true
Or
要么
params[:since].to_i
#1
1
get "/users/active_users/:since"
By making this routes its compulsory to give the params[:since]
, other wise it will throw back a error not routes matches
通过使这条路线成为强制给出params [:since],其他方面它会抛出错误而不是路线匹配
So here by i would suggest you to make routes
所以我建议你做路线
get "/users/active_users"
Since it's get
type request so its won't affect more, you can append params[:since]
in query with routes like this:-
因为它是获取类型请求所以它不会影响更多,你可以在查询中添加params [:since]与这样的路由: -
/users/active_users?since=1999
And at your controller you will get params[:since] = 1999
在你的控制器你会得到params [:since] = 1999
So far as i know it can't be enforce routes to accept only integer params
but it can be handle at controller side
据我所知,它不能强制路由只接受整数参数,但它可以在控制器端处理
params[:since].is_a? Integer
=> true
Or
要么
params[:since].to_i