开发者

Most simple and effective routes.rb for these models?

开发者 https://www.devze.com 2023-03-04 03:21 出处:网络
class Topic < ActiveRecord::Base has_many:posts end class Post < ActiveRecord::Base belongs_to :topic
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
0

精彩评论

暂无评论...
验证码 换一张
取 消