【发布时间】:2013-10-15 21:32:43
【问题描述】:
我正在尝试遵循 RailsGuides,http://edgeguides.rubyonrails.org/getting_started.html
现在,在我应该能够在我的博客站点中添加 cmets 的第 6.4 节中,我遇到了这个错误:
NoMethodError in Posts#show
Showing C:/Sites/blog/app/views/posts/show.html.erb where line #12 raised: </br>
undefined method `comments' for #<Post:0x3a99668
下面是我的 show.html.erb 中的 sn-p
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
下面是我的 cmets 控制器的 sn-p:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
redirect_to post_path(@post)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
下面是我的帖子控制器:
class PostsController < ApplicationController
def new
@post = Post.new
end
def create
@post = Post.new(params[:post].permit(:title, :text))
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.all
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :text))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :text)
end
end
请帮帮我,谢谢。
-阿古斯
【问题讨论】:
-
确保您已正确遵循同一指南的第 6.2 节。
-
是的,我做到了。事实上,我已经尝试从网站本身复制粘贴代码 sn-ps。
-
我可以看看你的 app/modelss/post.rb 文件吗?同时,您是否尝试重新启动服务器以防您在生产模式下运行。
-
我试图重新启动我的服务器,代码现在可以工作了。我不知道发生了什么。感谢您的帮助
-
我没有给出足够的声望点来投票给答案。但会做
标签: ruby-on-rails ruby methodnotfound