我在我的rails应用程序中使用friendly_id作为我的路线。确定字符串是否仅包含数字
我想确保一个页面不能被数字ID访问。
页是这样的访问 - www.myurl.com/myfriendlyidstub
的页面不应该是这样的访问 - www.myurl.com/12345(12345 myfriendlyidstub的ID)
我无法在friendly_id的文档中找到这样做的方法。
所以我正在考虑在我的控制器中添加一个方法来检查帕拉姆是否是数字
def edit
if params[:id].is_numeric? #I need to know how to write this method
not_found #not_found redirects to a 404
end
#rest of the edit action
end
两个问题 -
我怎样写“is_numeric”的方法?
这是做事的好方法吗?你能看到任何陷阱吗?
你可能想看看[这个链接](http://railsforum.com/viewtopic.php?id=19081)。我没有看到这种方法的问题,尽管我认为*如果你喜欢,你可以在路由本身中执行正则表达式。 – aishwarya 2012-01-10 12:11:26
另一方面,白名单将是比黑名单更好的方法(指定什么是有效的输入,而不是指定无效的输入) – 2012-01-10 12:45:44