rails 3 应用程序中没有方法错误 [英] No method error in rails 3 app
本文介绍了rails 3 应用程序中没有方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此错误:
undefined method `post_comments_path' for #<#<Class:0x1052a6e98>:0x1052a4be8>
Extracted source (around line #27):
24:
25: <% end%>
26:
27: <% form_for [@post, Comment.new] do |f| %>
28: <p>
29:
30: <%= f.label :name, "Author" %><br />
我的路线:
Myblog::Application.routes.draw do
root :to => 'posts#index'
resources :comments
resources :posts, :has_many => :comments
post.rb
class Post < ActiveRecord::Base
has_many :comments
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to :post
end
views/posts/show.html.erb
views/posts/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<b>Title:</b>
<%= @post.title %>
</p>
<p>
<b>Body:</b>
<%= @post.body %>
</p>
<h2>Comments</h2>
<% @post.comments.each do |c|%>
<p>
<b><%=h c.name %>said: </b>
<%= time_ago_in_words(c.created_at)%> ago
</p>
<p>
<%= c.body%>
</p>
<% end%>
<% form_for [@post, Comment.new] do |f| %>
<p>
<%= f.label :name, "Author" %><br />
<%= f.text_field :name %><br />
<%= f.label :body, "Comment Description" %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Add Comment" %>
</p>
<% end %>
<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
很困惑,因为我没有看到任何对 post_comments_path 的引用??
Confused as I don't see any reference to post_comments_path??
耙路线:
root /(.:format) {:action=>"index", :controller=>"posts"}
comments GET /comments(.:format) {:action=>"index", :controller=>"comments"}
comments POST /comments(.:format) {:action=>"create", :controller=>"comments"}
new_comment GET /comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_comment GET /comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
comment GET /comments/:id(.:format) {:action=>"show", :controller=>"comments"}
comment PUT /comments/:id(.:format) {:action=>"update", :controller=>"comments"}
comment DELETE /comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
posts POST /posts(.:format) {:action=>"create", :controller=>"posts"}
new_post GET /posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
post PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
post DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
推荐答案
您没有通过路由指定 post_comments_path.您不应该在路由(仅限模型)中使用 :has_many
,而是将它们嵌套.这应该可以解决问题:
You don't have a post_comments_path specified through your routes. You're not supposed to use :has_many
in routes (only models), but nest them instead. This should do the trick:
resources :posts do
resources :comments
end
这篇关于rails 3 应用程序中没有方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文