简体   繁体   中英

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).

<% 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.

In Rails 3, you can define nested routes as such:

resources :posts do
  resources :comments
end

I think you may also need to define form_for a little differently:

<%= 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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