无法访问rails 3.0.1中的嵌套资源“comments”

时间:2011-01-02 13:25:22

标签: routes ruby-on-rails-3 nested-forms formtastic

我希望你能帮助我。

/config/routes.rb

resources :deadlines do  
  resources :comments  
end

/model/comment.rb

class Comment < ActiveRecord::Base  

  belongs_to :post, :class_name => "Post", :foreign_key => "post_id"  

end  

/model/post.rb

class Post < ActiveRecord::Base  

  has_many :comments  

end

当我想访问http://localhost:3000/posts/1/comments/new时,我得到:

undefined method `comments_path' for #<#<Class:0x4889d18>:0x4887138> in _form.html  

我使用'formtastic'而the _form.html.erb看起来像这样:

<% semantic_form_for [@comment] do |form| %>  
  <% form.inputs do %>  
    <%= form.input :content %>  
  <% end %>  

  <% form.buttons do %>  
    <%= form.commit_button %>  
  <% end %>  
<% end %>

1 个答案:

答案 0 :(得分:1)

您的其他型号是Post还是Deadline?假设它是Post

resources :posts do
  resources :comments
end

在终端中运行rake routes以查看您的所有路线。更多信息:

嵌套资源的语法是:

<% semantic_form_for [@post, @comment] do |form| %>