0

我正在向我的站点添加一个博客,因此我创建了一个模型 bpost,其中包含变量标题、内容和类别。在 bpost 模型中,我重写了 to_param 函数:

def to_param
  {#id #title}.parametrize
end

所以现在所有的 url 看起来像:/bposts/2-to-be-or-not-to-be,其中“to be or not to be”是标题(2 是示例 id)。

我希望网址看起来像:/:category/2-:title。我试过了:

resources :bposts, :path => :category

或匹配 ':category/:id-:title', :to => :controller => :bposts, :action => :show

但似乎都不起作用。关于如何做到这一点的任何想法?

4

1 回答 1

0

查看路由指南:http: //guides.rubyonrails.org/routing.html

我想你想要的是一个嵌套资源,比如

资源: :category 做资源: :bposts end

我认为这:bposts是一个坏名字......让它postsblog_posts

您需要查看的另一件事是对宝石友好的 id:https ://rubygems.org/gems/friendly_id

于 2013-07-07T18:52:19.030 回答