CalendarPosts中的NoMethodError #index undefined method' title'为零:NilClass ...有时只出错?

时间:2018-05-10 19:45:20

标签: ruby-on-rails view null nomethoderror

Rails新手..我在我的联接表索引页面上收到错误..但有时只是。我很困惑,因为有时它有效,有时它不会。此外,当我进入pry并搜索引发错误的内容时,该项目就在那里。

连接表关系: 帖子有很多日历/日历有很多帖子 calendars_posts belongs_to日历 calendar_posts belongs_to posts

calendar_posts控制器:

  def index
@calendars = current_user.calendars

index.html.erb:

   <h1>Scheduled Posts</h1>

<% current_user.calendars.each do |calendar| %>
<h2> <%= calendar.name %> </h2>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Date</th>
      <th>Time</th>
      <th>Content</th>
      <th>Link</th>
      <th>Picture</th>
      <th>Platforms</th>
      <th>Finalized</th>
    </tr>
  </thead>

  <tbody>
    <% calendar.calendar_posts.each do |calendar_post| %>
    <tr>
      <td><%= calendar_post.post.title.titleize %> </td>
      <td>  <%= calendar_post.date%> </td>
      <td><%= calendar_post.time %></td>
      <td>  <%= calendar_post.post.content %> </td>
      <td>  <%= calendar_post.post.link %> </td>
      <td>  <%= image_tag(calendar_post.post.picture_url, width: 200) if calendar_post.post.picture.present? %> </td>
      <td>
        <% calendar_post.post.platforms.each do |platform| %>
        <%= platform.name.titleize %> <br>
        <% end %>
      </td>
      <td>  <%= human_boolean(calendar_post.post.finalized) %> </td>
    </tr>
    <% end %>
  </tbody>
</table>
<% end %>

我只是感到困惑,因为错误有时只会发生...但是一旦它开始发生,那么每当我尝试访问该页面后它就会发生?我能做些什么来抵御零呢?

1 个答案:

答案 0 :(得分:0)

据我所知,人们对此进行了投票,但我总是在发布之前进行研究,最终我发现了问题。它不像在相关搜索中出现的任何其他问题,所以它可能有助于其他人知道...在我的模型中,我有日历和帖子与连接表CalendarPosts与用户可提交的时间和日期属性。用户可以创建一个帖子,它会与所有内容分开浮动,因为我希望有人能够将想法记录为&#34;填充物&#34;而不是要求更进一步。这个nil:nilclass错误来自于我有一个依赖性问题。我的calendar_posts取决于帖子。但是我的帖子或日历模型中没有依赖性破坏。如果用户删除帖子,日历帖子仍然存在,但帖子没有,这就是问题所在。添加依赖性破坏修复了我的问题。