获取与 Stack Overflow 上相同的 url 结构 [英] Get same url structure as on Stack Overflow

查看:49
本文介绍了获取与 Stack Overflow 上相同的 url 结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于 SO 的问题具有以下网址结构:

A question on SO has this url structure:

http://stackoverflow.com/questions/18474799/changed-the-input-value-in-html5-datalist

如果我们假设 number 部分是 ID,那么前两个部分(域扩展后)可以通过在 routes.rb 中简单地使用以下内容获得

If we assume that the number section is the ID, the first two sections (after the domain extension) are obtained by simply using the following in routes.rb

资源:问题

问题已经由它的 ID 标识了,那么我们如何以最简单的方式添加(可选)装饰 slug 呢?我们是否需要使用新的链接助手(并包括额外的参数),或者可以在其他地方解析 3 节 url 吗?

The question is already identified by it's ID, so how do we add the (optional) decorating slug in the simplest of manners? Do we need to use a new link helper (and including additional params) or can the 3-section url be resolved elsewhere?

更新:

为了将这个问题更多地集中在路由处理上,让我们假设已经有一个 slug 作为属性保存在对象上(在创建时),例如@question.slug

To focus this question more on the route-handling, let's presume there is already a slug saved on the object (upon creation) as an attribute, e.g. @question.slug

如果 routes.rb 或/和控制器中的规则可以启用和处理可选的 slug,而不必在所有视图中编写长链接助手,那将是一个优势.

It would really be an advantage if a rule in routes.rb or/and in the controller could enable and handle the optional slug, instead of having to write long link helpers in all views.

推荐答案

resources :questions do
  member: title
end

对于 slug 使用 friendly_id 是的,别忘了看看 Rails 路由

for slug use friendly_id and yes don't forget to have a look at Rails Routing

这篇关于获取与 Stack Overflow 上相同的 url 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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