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

发布于 2024-11-05 07:41:12 字数 415 浏览 0 评论 0原文

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 的顺序是否正确?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

oО清风挽发oО 2024-11-12 07:41:12

当谈到路由时,“正确”是相当主观的。假设它正在生成您想要的路线,那么您这里的内容是完全有效的。这样,您将在帖子中嵌套评论,并在根目录中添加主题。您的另一个选择是将帖子嵌套在 URL 的主题中,如下所示:

/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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文