Rails 中 URL 中的 ID + Slug 名称(如 StackOverflow 中) [英] ID + Slug name in URL in Rails (like in StackOverflow)

查看:49
本文介绍了Rails 中 URL 中的 ID + Slug 名称(如 StackOverflow 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rails 中实现这样的 URL:

I'm trying to achieve URLs like this in Rails:

http://localhost/posts/1234/post-slug-name

同时使用 ID 和 slug 名称,而不是其中之一

with both ID and slug name instead of either

http://localhost/posts/1234

http://localhost/posts/post-slug-name

(现在我在 URL 中只有 slug 名称,所以这部分结束了).我该怎么做?

(right now I have just slug name in URL, so this part is over). How can I do this?

UPD

我找到了一篇关于此的文章:http://augustl.com/blog/2009/styling_rails_urls/,而不是 /id/slug 它建议使用 /id-slug 这对我来说非常适合,所以我会继续这样做.

I found an article on this: http://augustl.com/blog/2009/styling_rails_urls/, instead of /id/slug it suggests to use /id-slug which works perfectly for me, so I'll go with this.

推荐答案

您需要使用 Route Globbing 除了你的资源路由(当然假设你的 posts 路由是这样定义的).例如,

You'll want to add a regular route with Route Globbing in addition to your resource route (assuming of course that's how your posts routes are defined). For example,

map.resources :posts
map.connect '/posts/:id/*slugs', :controller => 'posts', :action => 'show'

这篇关于Rails 中 URL 中的 ID + Slug 名称(如 StackOverflow 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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