公开嵌套路由的特定操作
以此作为上下文示例: 帖子有_很多条评论 评论属于_帖子
我有一条如下所示的路线:
resources :posts do
resources :comments
end
如何创建公开 comments#index 的路线?
一个示例用例是......我想在页面上列出系统中的所有评论。本质上,当用户点击 /comments 时,使用评论资源就好像它没有嵌套一样,
谢谢!
Using this as an example contexted:
Post has_many comments
Comment belongs_to post
I have a route that looks like:
resources :posts do
resources :comments
end
How do i create a route that exposes comments#index?
An example use case would be... I want to list ALL comments in the system on a page. Essentially using the comments resource as if it's not nested when a user hits /comments
thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个。
也就是说,我通常会避免这样的路由,因为我喜欢整洁的 RESTful 路由文件,但有时这是没有帮助的。
第二个选项:
在第二个选项中,您需要从 comments 控制器中删除
index
操作,并在 posts 控制器中创建comments
操作。Try this.
That said, I usually look to avoid routes like this because I like a tidy RESTful routes file, but sometimes it can't be helped.
Second option:
In the second option, you'd want to remove the
index
action from the comments controller and create acomments
action in your posts controller.