Ruby on Rails:在资源路由上使用slug代替id [英] Ruby on Rails: Use slug instead of id on resource routes

查看:53
本文介绍了Ruby on Rails:在资源路由上使用slug代替id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在寻找一种解决方案,可以帮助我使用Rails资源实现以下目标:

So I'm looking for a solution that would help me achieve the following with Rails resource:

/admin/articles/:slug/edit

而不是

/admin/articles/:id/edit

I正在寻找Rails 资源路线,而不是其他类型的

I'm looking for Rails resource routes and not the other kinds of routes.

只是想知道是否有可能。如果是这样,如何?

Just wanted to know if it was possible. If so, how?

推荐答案

# config/routes.rb
resources :articles, param: :slug

在终端机中:

$ rake routes
...
article GET    /articles/:slug(.:format)      articles#show
...

这篇关于Ruby on Rails:在资源路由上使用slug代替id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆