简体   繁体   English

Rails:nil:NilClass的未定义方法

[英]Rails: undefined method for nil:NilClass

This might be a simple question, but I can't figure it out. 这可能是一个简单的问题,但我无法弄清楚。

Model Question has many Model Answer , and they both belong to Model User . 模型Question有许多模型Answer ,它们都属于模型User

Here is part of index.erb.html : 这是index.erb.html一部分:

<tbody>
    <% @questions.each do |question| %>
      <tr>
        <td><%= question.content %></td>
        <%= time_ago_in_words(question.created_at) %> ago by <%= question.user.email %>

        <td><%= link_to 'Show', question %></td>
        <td><%= link_to 'Edit', edit_question_path(question) %></td>
        <td><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %></td>

        <% if question.answers %>
          <h4>Answers</h4>
          <% @question.answers.each do |answer| %>
          <p> 
             <%= answer.content %> 
          </p>
          <% end %>
        <% end %>

        <br>
        <p>Add Answers</p>ww
        <%= form_for([question,question.answers.build]) do |f| %>
          <%= f.text_area :content %>
          <%= submit_tag "Add" %>
          <br>
          <% end %>

      </tr>
    <% end %>
  </tbody>

And I used seed.rb to insert data. 然后我使用seed.rb插入数据。

Here is seed.rb 这是seed.rb

User.destroy_all
Question.destroy_all
Answer.destroy_all

user = User.create(email: "123@gmail.com", password: "2wsx1qaz")
q = Question.create(content: "Q1", user: user)
#q2 = Question.create(content: "Q2", user: user)
Answer.create(content:  "answer1", question: q, user: user)
Answer.create(content: "answer2", question: q, user: user)

But it threw this error 但是它抛出了这个错误 在此处输入图片说明

I also put this project on github 我也把这个项目放在github上

Update 更新资料

After above error, the render result is weird. 发生上述错误后,渲染结果很奇怪。

It should show Question, and then check there are any answer for that question. 它应该显示“问题”,然后检查该问题是否有答案。 If there are, shows answers. 如果有,则显示答案。

But, the result on my browser is like that 但是,在我的浏览器上的结果是这样的

在此处输入图片说明

But, I expected it should be like this 但是,我希望它应该像这样

在此处输入图片说明

You don't have @question defined, so it's nil and that's why you got: undefined method answers for nil:NilClass when you called this: 您没有定义@question ,所以它为nil ,这就是为什么您得到了:调用此undefined method answers for nil:NilClassundefined method answers for nil:NilClass

@question.answers

You actually have question defined. 您实际上已经定义了question So, use that :-) 因此,使用它:-)

Change this: 更改此:

<% @question.answers.each do |answer| %>

To: 至:

<% question.answers.each do |answer| %>

I think this line is incorrect 我认为这行是不正确的

<%= time_ago_in_words(question.created_at) %> ago by <%= question.user.email %>

which is outside table data so put this in your <td> clause. 它在表数据之外,因此将其放在您的<td>子句中。 an your answers are also not in any table data so put it in proper format of table. 您的答案也不在任何表格数据中,因此请以表格的正确格式显示。 for more info to render table in html #table . 有关更多信息,以在html #table中呈现表。 so it is disturbed your UI part. 因此它会干扰您的UI部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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