2

我在我的 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”方法?

这是做事的好方法吗?你能看出什么陷阱吗?

4

2 回答 2

4

before_filter在将其粘贴到每种方法中之前,我都会使用它。

您还可以配置路由段约束

于 2012-01-10T12:13:49.157 回答
0

听起来您只想在根“/”之后将用户名作为永久链接...

config/route.rb
...
match '/:id' => 'controller#show'
...

之后,我相信如果您正确设置 Friendly It,它应该显示为名称而不是 id。

顺便说一句,感谢您找到friendly_id,我正要开始为我的项目寻找类似的东西。

让我知道这是否有帮助。

于 2012-01-10T12:59:16.980 回答