带模型变量的Rails路由

时间:2013-07-07 16:07:26

标签: ruby-on-rails-3 routes

我正在为我的网站添加一个博客,所以我创建了一个模型bpost,其中包含变量标题,内容和类别。在比特犬模型中,我覆盖了to_param函数:

def to_param
  {#id #title}.parametrize
end

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

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

resources :bposts, :path => :category

OR     匹配':category /:id-:title',:to => :controller => :bposts,:action => :显示

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

1 个答案:

答案 0 :(得分:0)

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

我想你想拥有的是像

这样的嵌套资源

资源::类别做   资源:: bposts 端

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

另一件值得关注的是宝石友好ID:https://rubygems.org/gems/friendly_id