开发者

Help convert a nested route to Rails 3

开发者 https://www.devze.com 2023-01-12 10:22 出处:网络
I have a comment form (in comments/_form.html.erb) that I use in my other controllers (posts and tags).

I have a comment form (in comments/_form.html.erb) that I use in my other controllers (posts and tags).

<% form_for([@post, Comment.n开发者_如何转开发ew], :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.

0

精彩评论

暂无评论...
验证码 换一张
取 消