判断字符串是否只包含数字
我在 Rails 应用程序中使用 friend_id 作为我的路线。
我想确保页面不能被数字id访问。
页面可以像这样访问 - www.myurl.com/myfriendidstub
该页面不应该像这样访问 - www.myurl.com/12345 (12345 是 myfriendidstub 的 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”方法?
这是一个好的做事方式吗?你能看到任何陷阱吗?
I'm using friendly_id for my routes in my rails app.
I want to ensure that a page cannot be visited by the numeric id.
A page is accessible like this - www.myurl.com/myfriendlyidstub
The page should not be accessible like this - www.myurl.com/12345 (12345 is the id of myfriendlyidstub)
I can't see a way to do this in friendly_id's docs.
So I'm considering adding a method in my controller to check whether the param is numeric
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
Two questions -
How do I write the 'is_numeric' method?
Is this a good way of doing things? Can you see any pitfalls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在将其粘贴到每个方法之前,我会使用
before_filter
。您还可以配置路由分段约束。
I'd use a
before_filter
before sticking it in every method.You may also configure a routing segment constraint.
听起来您只想将用户名作为根“/”之后的永久链接...
之后我相信,如果您正确设置了Friendly It,它应该显示为名称而不是ID。
顺便说一句,感谢您找到Friendly_id,我正准备开始为我的项目寻找类似的东西。
让我知道这是否有帮助。
It sounds like you just want to have user names as a permalink right after the root '/'...
After that I believe if you setup Friendly It correctly it should display as a name instead of an id.
btw thanks for finding friendly_id, I was about to start looking for something like that for my project.
Let me know if that helps.