2016-11-05 43 views
0

我正在做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

+0

这只是你在那个特定时间没有与该帖子相关的群组。如果你想忽略它,你可以使用'post.group.try(:title)'。如果组为'nil',则不会引发错误。 –

回答

1

你需要确保有一个与post相关的group,您可以使用try只为呈现纯内容(所以它呈现什么都没有,但不会引发异常):

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

而且if控制流有条件地呈现链接:

<% 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%> 
相关问题