Netnuts 教程中的 Rails 3 主题#new 中的 NoMethodError(nil:NilClass 的未定义方法“内容”)。
我一直在 net.tutsplus 论坛教程,但我遇到了一些问题。评论全部变成了垃圾邮件,文章也被遗忘了。
我已按照教程进行操作,并在发布新主题时得到以下信息:
NoMethodError in Topics#new
Showing /var/www/app/views/topics/_form.html.erb where line #9 raised:
undefined method `content' for nil:NilClass
Extracted source (around line #9):
6: <%= f.text_field :name %>
7: </p>
8: <p>
9: <textarea name="post[content]" cols="80" rows="20"><%= @post.content %></textarea>
10: </p>
11: <p><%= f.submit "Create" %></p>
12: <% end %>
Trace of template inclusion: app/views/topics/new.html.erb
我的模型/控制器和视图已按照教程的指示创建。
你能帮忙吗?
I have been following this tutorial over at net.tutsplus Forum tutorial but I have run into some issues. The comments have all turned to Spam and the article has been forgotten.
I have followed the tutorial and on posting a new topic i get the following:
NoMethodError in Topics#new
Showing /var/www/app/views/topics/_form.html.erb where line #9 raised:
undefined method `content' for nil:NilClass
Extracted source (around line #9):
6: <%= f.text_field :name %>
7: </p>
8: <p>
9: <textarea name="post[content]" cols="80" rows="20"><%= @post.content %></textarea>
10: </p>
11: <p><%= f.submit "Create" %></p>
12: <% end %>
Trace of template inclusion: app/views/topics/new.html.erb
My models/controllers and views have been created as instructed by the tutorial.
Can you help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误报告明确显示:
@post
为零,因此您无法对其调用.content
。由于您正在调用 new 操作,因此控制器中可能缺少
@post = Post.new
。我不熟悉本教程,但我建议使用类似于第 6 行的内容重构字段第 9 行,它使用 Rails 表单辅助方法。 即
Explicitly from the error report:
@post
is nil and therefore you cannot call.content
on it.Since you are calling a new action, you probably are just missing
@post = Post.new
in the controller.I am unfamiliar with the tutorial, but I would suggest refactoring field line 9 with something more like line 6, which uses the rails form helper methods. i.e.
您的帖子对象尚未初始化,您必须
在此之前完成。
Your post object is uninitalized, you must do
before.