这些模型最简单有效的路径.rb?

时间:2011-05-07 06:49:10

标签: ruby-on-rails routing model

class Topic < ActiveRecord::Base
  has_many   :posts
end

class Post < ActiveRecord::Base
  belongs_to :topic
  has_many   :comments
end

class Comment < ActiveRecord::Base
  has_ancestry
  belongs_to :post
end

MyApp::Application.routes.draw do
  resources :posts do
    resources :comments
  end
  resources :topics
  root :to => "posts#index"
end

我的routes.rb是否按正确的顺序?

1 个答案:

答案 0 :(得分:1)

说到路由,“正确”是非常主观的。假设它产生了你想要的路线,你在这里拥有的是完全有效的。有了这个,您将在帖子中嵌套注释,并在根目录中添加主题。您还有另一个选择,即在主题中嵌套帖子,其内容类似于:

/topics/1/posts
/topics/1/posts/1

进行以下更改:

MyApp::Application.routes.draw do
  resources :posts do
    resources :comments
  end

  resources :topics do
    resources :posts do
  end

  root :to => "posts#index"
end