开发者

NoMethodError in Topics#new (undefined method `content' for nil:NilClass) for Rails 3 from netnuts tutorial.

开发者 https://www.devze.com 2023-04-02 09:05 出处:网络
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 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 "Creat开发者_如何学Goe" %></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?


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 %>


Your post object is uninitalized, you must do

@post = Post.new

before.

0

精彩评论

暂无评论...
验证码 换一张
取 消