帮助将嵌套路由转换为 Rails 3
我有一个评论表单(在 comments/_form.html.erb 中),我在其他控制器(帖子和标签)中使用它。
<% form_for([@post, Comment.new], :html => { :multipart => true }) do |f| %>
<%= f.text_field :commenter %>
<%= f.text_field :email %>
<%= f.text_area :body %>
<%= f.submit 'submit' %>
<% end %>
在我的 Comment 模型中,我有:
belongs_to :post
在我的应用程序的 Rails 2 版本中,我的 routes.rb
包含 map.resources :posts, :has_many => :comments
工作正常,但 Rails 3 中的相同配置会引发未定义的方法错误:
undefined method `post_comments_path' for #<#<Class:0xf94920>:0xf8d540>
我认为 Rails 2.x 路由只是贬值,直到 3.1 发布。如何将其转换为 Rails 3?感谢您阅读我的问题。
I have a comment form (in comments/_form.html.erb) that I use in my other controllers (posts and tags).
<% form_for([@post, Comment.new], :html => { :multipart => true }) do |f| %>
<%= f.text_field :commenter %>
<%= f.text_field :email %>
<%= f.text_area :body %>
<%= f.submit 'submit' %>
<% end %>
In my Comment model I have:
belongs_to :post
In the rails 2 version of my application my routes.rb
included map.resources :posts, :has_many => :comments
which worked fine but the same configuration in Rails 3 throws an undefined method error:
undefined method `post_comments_path' for #<#<Class:0xf94920>:0xf8d540>
I thought Rails 2.x routes were just depreciated until 3.1 comes out. How do I convert this to Rails 3? Thanks for reading my question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Rails 3中,您可以这样定义嵌套路由:
我认为您可能还需要稍微不同地定义form_for:
希望有帮助!查看 http://rizwanreza.com/2009/12/ 20/revamped-routes-in-rails-3 了解有关 Rails 3 中路由的更多信息。
In Rails 3, you can define nested routes as such:
I think you may also need to define form_for a little differently:
Hope that helps! Check out http://rizwanreza.com/2009/12/20/revamped-routes-in-rails-3 for a bit more information about routing in Rails 3.