2016-11-05 8 views
0

Ruby101 튜토리얼을 진행하고 있지만 잘못된 것이 있습니다.NoMethodError in Account :: Posts # 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/rescues/template_error.html.erb 구조/레이아웃 내의 렌더링 /home/zedong/.rvm/gems/ruby-2.3.1/ gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb 렌더링 됨 /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) 렌더링 /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack -5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb 렌더링 됨 /home/zedong/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/ 미들웨어/템플릿/rescues/_trace.html.erb (2.5ms) 렌더링 /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/layo 유타 (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 
내가 두 번째 시간이 자습서를하고 있어요 때문에

, 그래서 비교 그것은 처음으로 코드와 함께. 그리고 문제를 찾기 위해 하나씩 파일을 복사하려고 시도했지만 작동하지 않습니다. 그건 그렇고, 내가 버튼을 삭제하고 구현하려는 경우, 뭔가 잘못되었습니다. 여기

프로젝트 : github 당신은 그냥 평범한 내용을 렌더링 try을 사용할 수 있습니다 (그래서 아무것도 렌더링,하지만 예외를 발생시키지 않습니다는 post과 관련된 group이 있는지 확인해야합니다

+0

그 특정 시간에 해당 게시물과 관련된 그룹이 없다는 것입니다. 그것을 무시하고 싶다면,'post.group.try (: title)'을 사용할 수 있습니다. group이'nil'이면 에러를 발생시키지 않습니다. –

답변

1

) :

<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%>