创建留言板时遇到多个资源问题
我正在为我的应用程序开发一个留言板,它具有使其正常工作所需的三种典型资源:论坛、主题和帖子。路由定义如下:
resources :forums do
resources :topics do
resources :posts
end
end
主题将帖子定义为嵌套资源,因为当您创建新主题时,会创建一个新帖子来启动线程。这样一切都运转良好。但我被困在主题的“显示”页面上。在该页面上,与任何留言板模型一样,显示所有帖子后,底部有一个表单可用于添加新帖子/回复。一旦触发帖子的创建操作,它将重定向回主题等。实现该表单就是我所遇到的问题。
通常我会想到这样的事情......
<%= form_for([@topic, @post]) do |f| %>
...
<% end %>
只有页面返回“未定义的方法‘topic_posts_path’”错误。这并不奇怪,因为查看 rake 路由会将其返回为帖子的创建操作。
forum_topic_posts POST /forums/:forum_id/topics/:topic_id/posts(.:format) {:controller=>"posts", :action=>"create"}
想我会尝试 <%= form_for([@forum, @topic, @post]) do |f| %>在 form_for 中,但只是返回了相同的错误。考虑到我需要给它实际的路径,我给了 <%= form_for :url => forum_topic_posts_url 做 |f| %>尝试了一下,但它也不关心这一点。
谷歌搜索后,大多数论坛教程都已经过时,或者没有在主题页面上添加新的帖子表单,只显示我已经解决的几乎所有内容。
我能提出的唯一另一件事是建议不要将资源嵌套到三级或更多级别,但没有给出“为什么”,更不用说提出合适的替代方案了。此外,上面的路线似乎是此类操作最合乎逻辑的路径。
那么,在这种设置中,如何才能使帖子表单在“主题显示”页面上正常工作呢?
I'm working on a message board for my application and it has the typical three resources to make it all work: Forums, Topics and Posts. The routes are defined as so:
resources :forums do
resources :topics do
resources :posts
end
end
Topics have posts defined as a nested resource, as when you create a new topic, a new post is created to start the thread off. So that all works well. But I'm stuck on the Topic's 'Show' page. On that page, like any message board model, after showing all the posts there is a form at the bottom to add a new post / reply. Once the post's create action is triggered, it will redirect back to the topic, etc. Implementing that form is what I am tripped up on.
Normally I would think of something like this...
<%= form_for([@topic, @post]) do |f| %>
...
<% end %>
...only the page returns an "undefined method `topic_posts_path'" error. And that's not surprising, as taking a look at rake routes returns this as the create action for Posts.
forum_topic_posts POST /forums/:forum_id/topics/:topic_id/posts(.:format) {:controller=>"posts", :action=>"create"}
Figured I would try <%= form_for([@forum, @topic, @post]) do |f| %> in the form_for, but just returned me the same error. Figuring I needed to give it the actual path instead, I gave <%= form_for :url => forum_topic_posts_url do |f| %> a try, but it didn't care for that either.
After Googling, most forum tutorials were outdated or stopped short of adding the new posts form on the topic page, only showing nearly all the stuff I worked out already.
The only other thing I was able to turn up suggested not nesting resources three or more levels down, but stopped short of 'why', much less suggest a suitable alternative. Besides, the route path above seems like the most logical path for this kind of action.
So how do I accomplish getting the Post form to work on the Topic Show page in this kind of setup?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您尝试“form_for([@forum, @topic, @post])”时,您是否在控制器中设置了@forum变量?
When you try "form_for([@forum, @topic, @post])" you have set @forum variable in controller?