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
post.group.try(:title)
. If group isnil
, it won't raise an error. – Alexandre Angelim