has_many,belongs_to和评论表格

时间:2010-04-16 16:19:21

标签: ruby-on-rails rest routing

我有两个模型:SnippetSnippetComment。它们也有自己的控制器和视图。在/app/views/snippets/show.html.erb我有一个以这样开头的表单:

<% form_for(@new_snippet_comment) do |form| %>

SnippetComment属于一个SnippetSnippet属于一个User。这意味着我有这个路由(making it non-RESTful == making it suck,如你所知):

map.resources :users do |user|
  user.resources :snippets do |snippet|
    snippet.resources :snippet_comments, :as => "comments"
  end
end

因此,当我在SnippetComment视图中提交SnippetController#show表单时,应该提出此请求:

POST /users/x/snippets/x/comments HTTP/1.1

(其中xUserSnippet的{​​{1}})。

问题在于我甚至没有收到评论提交表格,但是对此表示赞同:

  

Snippets#show

中的NoMethodError      

显示   app / views / snippets / show.html.erb在哪里   第29行提出:

id
     

     

提取的来源(约undefined method `snippet_comments_path' for 行):

#29
     

RAILS_ROOT:   /用户/ jeffatwood /开发/ youjustdontneedmyrealname

     

应用程序跟踪|框架跟踪|   完整跟踪

26: <% if current_user %>
27: <h2>Schrijf een nieuwe reactie</h2>
28: 
29: <% form_for(@new_snippet_comment) do |form| %>
30: 
31: <p>
32:   <%= form.text_area :body %>
     

请求

     

参数:

/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `__send__'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:107:in `polymorphic_url'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/polymorphic_routes.rb:114:in `polymorphic_path'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:298:in `apply_form_for_options!'
/Users/jeffatwood/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:277:in `form_for'
/Users/jeffatwood/Dev/youjustdontneedmyrealname/app/views/snippets/show.html.erb:29:in `_run_erb_app47views47snippets47show46html46erb'
/Users/jeffatwood/Dev/youjustdontneedmyrealname/app/controllers/snippets_controller.rb:19:in `show'
     

显示会话转储

     

响应

     

页眉:

{"id"=>"1",
 "user_id"=>"2"}

哦,在{"Content-Type"=>"text/html", "Cache-Control"=>"no-cache"} 我有这个:

snippets_controller.rb

任何人都可以帮我解决这个问题吗?感谢

2 个答案:

答案 0 :(得分:0)

您应该在创建snipped_id

时通过@new_snippet_comment

答案 1 :(得分:0)

您有两个级别的嵌套(即用户 - &gt;代码段 - &gt;评论),因此您的form_for语法应如下所示:

form_for [@user, @snippet, @comment]