简体   繁体   English

帮助将嵌套路由转换为Rails 3

[英]Help convert a nested route to Rails 3

I have a comment form (in comments/_form.html.erb) that I use in my other controllers (posts and tags). 我有一个评论表(在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 %>

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: 在我的应用程序的rails 2版本中,我的routes.rb包括map.resources :posts, :has_many => :comments工作正常但Rails 3中的相同配置引发了未定义的方法错误:

undefined method `post_comments_path' for #<#<Class:0xf94920>:0xf8d540>

I thought Rails 2.x routes were just depreciated until 3.1 comes out. 我认为Rails 2.x路线只是折旧,直到3.1出来。 How do I convert this to Rails 3? 我如何将其转换为Rails 3? Thanks for reading my question. 感谢您阅读我的问题。

In Rails 3, you can define nested routes as such: 在Rails 3中,您可以定义嵌套路由:

resources :posts do
  resources :comments
end

I think you may also need to define form_for a little differently: 我想你可能还需要以不同的方式定义form_for:

<%= form_for [:post, @comment] do |f| %>

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. 有关Rails 3中路由的更多信息,请查看http://rizwanreza.com/2009/12/20/revamped-routes-in-rails-3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM