简体   繁体   中英

undefined method `comments' for nil:NilClass in Rails

I'm adding comments to my Questions model, and I've run into an error.

When viewing the page, I get this error:

undefined method `comments' for nil:NilClass

The error is resulting from line 30:

30: <% @question.comments.each do |comment| %>

I can't figure out what's happening, though.

My controllers, models, and view is in this Gist

Could you help me out here?

You don't have @question variable. Sth like this:

@question = Question.find(params[:question_id])

In the proper method of your view - You didn't write is it a show, or maybe index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM