Netnuts 教程中的 Rails 3 主题#new 中的 NoMethodError(nil:NilClass 的未定义方法“内容”)。

发布于 2024-12-03 08:16:03 字数 823 浏览 4 评论 0原文

我一直在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2024-12-10 08:16:03

错误报告明确显示: @post 为零,因此您无法对其调用 .content

由于您正在调用 new 操作,因此控制器中可能缺少 @post = Post.new

我不熟悉本教程,但我建议使用类似于第 6 行的内容重构字段第 9 行,它使用 Rails 表单辅助方法。

<%= f.text_area :content, :cols => 80, :rows => 20 %>

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.

<%= f.text_area :content, :cols => 80, :rows => 20 %>
_蜘蛛 2024-12-10 08:16:03

您的帖子对象尚未初始化,您必须

@post = Post.new

在此之前完成。

Your post object is uninitalized, you must do

@post = Post.new

before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文