我正在建立一个博客平台(RoR),并计划使用以下格式,有什么缺点吗?
# All Users:
http://www.example.com/users/
# A single user (123 is the user id, id is needed for uniqueness)
http://www.example.com/users/123/peter
# All Categories
http://www.example.com/categories/
# A single category listing (123 is the cat id, note: job is singular)
http://www.example.com/categories/123/job
# All Tags
http://www.example.com/tags/
# A single tag listing (123 is the tag id, note: car is singular)
http://www.example.com/tags/123/car
# A single post
http://www.example.com/posts/123/my-title有什么建议或需要改进的地方吗?
谢谢。
发布于 2012-06-01 18:31:11
这完全取决于你的应用程序中最重要的主题是什么。假设应用程序的主要主题是帖子,那么帖子的url必须“更接近”根url。
所以它们看起来是这样的:
# A single post
http://www.example.com/123-my-post-title
# All Users:
http://www.example.com/users/
# A single user (supposing the username must be unique)
http://www.example.com/users/peter
# All Categories
http://www.example.com/categories/
# A single category listing (supposing the category name must be unique)
http://www.example.com/categories/job
# All Tags
http://www.example.com/tags/
# A single tag listing (supposing tags must be unique)
http://www.example.com/tags/car这涉及到您的路由的两个更改:
1)去掉URL中的“/post/”。为此,您可以像这样声明您的资源:
resources :posts, :path => "/"2)删除除post#show以外的所有URL中的id (来自不同用户的帖子有时可能具有相同的标题,如果不同,您也可以在这些情况下省略ID )。
要做到这一点,应该覆盖模型中的to_param方法,如下所示:
class Post < ActiveRecord::Base
def to_param
"{id}-#{title.parameterize}"
end
end或者,如果您发现覆盖to_param很困难,请使用friendly_id gem。
发布于 2012-05-28 19:23:59
我也遇到过同样的问题,我遇到了一个让你的urls搜索引擎优化且对人友好的好东西:friendly_id。
这里有一个很棒的开始它的截屏视频:Pretty URLs with FriendlyId。
我不打算在这里详细介绍,因为我相信你会在文档或截屏视频中找到你需要的一切。搜索引擎优化快乐!
发布于 2012-05-28 19:28:01
因为(我假设)用户有唯一的昵称,所以url可以更简单:
http://www.example.com/users/peter同样的情况也适用于标签和类别。您只是不要忘记检查基于参数化的名称的唯一性。
它还有助于避免具有相似名称的对象的重复:
"peter the great".parameterize => "peter-the-great"
"Peter-THE-Great".parameterize => "peter-the-great" 这只是我的观点,在大多数情况下,使用带有唯一id-name或id/name的逻辑(看看这个问题的url :)。
https://stackoverflow.com/questions/10783173
复制相似问题