0
votes

I'm doing Ruby101 tutorial, but get something wrong.

The rails log:

ActionView::Template::Error (undefined method `title' for nil:NilClass):
13:       <% @posts.each do |post| %>
14:         <tr>
15:           <td> <%= post.content %> </td>
16:           <td> <%= post.group.title %> </td>
17:           <td> <%= post.updated_at %> </td>
18:           <td> <%= link_to('Edit', edit_group_post_path(post.group, post), class: "btn btn-default btn-xs") %></td>
19:           <td> <%= link_to('Delete', group_post_path(post.group, post), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default btn-xs") %></td>

app/views/account/posts/index.html.erb:16:in block in _app_views_account_posts_index_html_erb___92982360307258762_69918747126320' app/views/account/posts/index.html.erb:13:in_app_views_account_posts_index_html_erb___92982360307258762_69918747126320' Rendering /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout Rendering /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb Rendered /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.7ms) Rendering /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.5ms) Rendering /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb Rendered /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms) Rendered /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (21.5ms)

And the group.rb:

class Group < ActiveRecord::Base
  belongs_to :user
  has_many :posts
  validates :title, presence: true
  has_many :group_relationships
  has_many :members, through: :group_relationships, source: :user
end

The post.rb:

class Post < ApplicationRecord
  validates :content, presence: true
  belongs_to :user
  belongs_to :group

  scope :recent, -> {order("created_at DESC")}
end

Because I'm doing this tutorial at second time, so I compare it with the first time's code. And I have tried to copy the file one by one to find the problem, but it can't works. By the way, when i want to implement the eidt and delete button, something was wrong.

Project at here: github

1
It's just that you don't have a group associated with that post at that particular time. If you'd like to ignore that, you can use post.group.try(:title). If group is nil, it won't raise an error.Alexandre Angelim

1 Answers

1
votes

You need to ensure that there is a group associated with the post, you can use try just for rendering the plain content (so it renders nothing, but doesn't raise an exception):

<td> <%= post.group.try(:title) %> </td>

And if control flow to conditionally render links:

<% if post.group.present? %>
  <td> <%= link_to('Edit', edit_group_post_path(post.group, post), class: "btn btn-default btn-xs") %></td>
  <td> <%= link_to('Delete', group_post_path(post.group, post), method: :delete, data: { confirm: "Are you sure?" }, class: "btn btn-default btn-xs") %></td>
<%end%>