ruby-on-rails – 博客平台的推荐URL格式
发布时间:2020-12-17 02:36:07 所属栏目:百科 来源:网络整理
导读:我正在构建一个博客平台(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 Categorieshttp://www.example.com
我正在构建一个博客平台(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 有什么建议或改进的地方吗? 谢谢. 解决方法
这完全取决于你的应用程序最重要的主题是什么.假设应用程序的主要主题是帖子,那么帖子的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 这涉及您的路线中的2个变化: 1)删除网址中的“/ posts /”.为此,您可以像这样声明您的资源: 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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |