这些模型最简单有效的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
我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当谈到路由时,“正确”是相当主观的。假设它正在生成您想要的路线,那么您这里的内容是完全有效的。这样,您将在帖子中嵌套评论,并在根目录中添加主题。您的另一个选择是将帖子嵌套在 URL 的主题中,如下所示:
进行以下更改:
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:
With the following change: