我正在做Ruby101教程,但出錯了。NoMethodError在帳戶::帖子#index
軌道日誌:
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>
應用程序/視圖/帳號/職位/ index.html.erb:16:
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' 渲染/home/zedong/.rvm/gems/ruby -2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescue/template_error.html.erb in rescue/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 呈現/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 ) 渲染/home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb內的救援/ layo UT(21.5ms)
而且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
的post.rb:
class Post < ApplicationRecord
validates :content, presence: true
belongs_to :user
belongs_to :group
scope :recent, -> {order("created_at DESC")}
end
因爲我在做這個教程的第二次,所以我比較它與第一次的代碼。我試圖一個接一個地複製文件來發現問題,但它不能工作。順便說一下,當我想實現eidt和刪除按鈕時,出現了一些錯誤。
項目在這裏:github
這只是你在那個特定時間沒有與該帖子相關的羣組。如果你想忽略它,你可以使用'post.group.try(:title)'。如果組爲'nil',則不會引發錯誤。 –