Most simple and effective routes.rb for these models?
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
Is my routes.rb in the correct order?
When it comes to routing, "correct" is pretty subjective. What you have here is totally valid, assuming it's producing the routes that you want. With this, you will have comments nested within posts, and topics at the root. Another option you have is to also nest posts within topics for URLs that looks something like:
/topics/1/posts
/topics/1/posts/1
With the following change:
MyApp::Application.routes.draw do
resources :posts do
resources :comments
end
resources :topics do
resources :posts do
end
root :to => "posts#index"
end
精彩评论