2015-01-27 5 views
0

Ruby 사용 경험이없고 다음 문제가 발생했습니다. 로그인하려고하면 "죄송하지만 뭔가 잘못되었습니다"오류 메시지가 표시됩니다. 관리자 패널 (mydomain.com/administrator).정의되지 않은 메서드 Ruby 2.1의 오류 메시지

<h1><%= t(".title")%></h1> 

<!-- Pages --> 
<% if @pages.length > 0 %> 
    <h3><%= t(".lastets_pages")%></h3> 
    <table>  
    <thead>  
     <tr>  
     <th>#</th>  
     <th><%=t("activerecord.attributes.page.title")%></th>  
     <th><%=t("activerecord.attributes.page.slug")%></th>  
     <th><%=t("generic.user_id")%></th>  
     <th><%=t("generic.created_at")%></th>  
     <th><%=t("generic.updated_at")%></th>  
     <th><%=t("generic.actions")%></th>  
     </tr> 
    </thead> 
    <tbody>  
     <% @pages.each do |page| %>  
     <tr>  
      <td><%= page.id %></td>  
      <td><%= page.title %></td>  
      <td><%= page.slug %></td>  
      <td><%= page.user.username %></td>  
      <td><%= l(page.created_at, format: :long) %></td>  
      <td><%= l(page.updated_at, format: :long) %></td>  
      <td class="actions_links"> 
      <%= link_to t("generic.edit"), edit_administrator_page_path(page) %> 
      <%= link_to t("generic.delete"), administrator_page_path(page), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>  
      </td>  
     </tr>  
     <% end %> 
    </tbody> 
    </table> 
<% else %> 
    <h3><%= t(".lastets_pages")%></h3> 
    <div class="alert alert-warning">  <%= t(".lastets_pages_empty")%> </div> 
<% end %> 

<!-- Notices --> 
<% if @notices.length > 0 %>  
    <h3><%= t(".lastets_notices")%></h3> 
    <table>   
    <thead>  
     <tr> 
     <th>#</th> 
     <th><%=t("activerecord.attributes.notice.title")%></th> 
     <th><%=t("activerecord.attributes.notice.countries")%></th> 
     <th><%=t("activerecord.attributes.notice.slug")%></th> 
     <th><%=t("generic.user_id")%></th> 
     <th><%=t("generic.created_at")%></th> 
     <th><%=t("generic.updated_at")%></th> 
     <th><%=t("generic.actions")%></th>  
     </tr>  
    </thead>  
    <tbody>    
     <% @notices.each do |notice| %>  
     <tr> 
      <td><%= notice.id %></td> 
      <td><%= notice.title %></td>   
      <td><%= notice.show_countries %></td> 
      <td><%= notice.slug %></td> 
      <td><%= notice.user.username %></td> 
      <td><%= l(notice.created_at, format: :long) %></td> 
      <td><%= l(notice.updated_at, format: :long) %></td> 
      <td class="actions_links"> 
      <%= link_to t("generic.edit"), edit_administrator_notice_path(notice) %> 
      <%= link_to t("generic.delete"), administrator_notice_path(notice), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %> 
      </td>  
     </tr>  
     <% end %>  
    </tbody> 
    </table> 
<% else %> 
    <h3><%= t(".lastets_notices")%></h3> 
    <div class="alert alert-warning">  <%= t(".lastets_notices_empty")%> </div> 
<% end %> 

<!-- Faqs --> 
<% if @faqs.length > 0 %> 
    <h3><%= t(".lastets_faqs")%></h3> 
    <table>   
    <thead>  
     <tr> 
     <th>#</th> 
     <th><%=t("activerecord.attributes.faq.title")%></th> 
     <th><%=t("activerecord.attributes.faq.slug")%></th> 
     <th><%=t("generic.user_id")%></th> 
     <th><%=t("generic.created_at")%></th> 
     <th><%=t("generic.updated_at")%></th> 
     <th><%=t("generic.actions")%></th>  
     </tr>  
    </thead>  
    <tbody>    
     <% @faqs.each do |faq| %>  
     <tr> 
      <td><%= faq.id %></td> 
      <td><%= faq.title %></td> 
      <td><%= faq.slug %></td> 
      <td><%= faq.user.username %></td> 
      <td><%= l(faq.created_at, format: :long) %></td> 
      <td><%= l(faq.updated_at, format: :long) %></td> 
      <td class="actions_links"> 
      <%= link_to t("generic.edit"), edit_administrator_faq_path(faq) %> 
      <%= link_to t("generic.delete"), administrator_faq_path(faq), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %> 
      </td>  
     </tr>  
     <% end %>  
    </tbody> 
    </table> 
<% else %> 
    <h3><%= t(".lastets_faqs")%></h3> 
    <div class="alert alert-warning">  <%= t(".lastets_faqs_empty")%> </div> 
<% end %> 

주의 사항 : 템플릿이 여기에 enter image description here

: production log

뿐만 아니라, 나는 login_controller.rb를 확인 : 다음 내가 발견 로그를 확인

. 모델 : notice.rb model

문제를 식별하는 데 도움을 줄 수 있습니까? 캐시를 지우려고했는데 아무 일도 없었습니다.

+1

스크린 샷을 읽기가 어렵습니다. 메소드 오류가 발생하지 않은 템플릿 (/administrator/login/dashboard.html.erb)의 내용을 게시해야합니다. 사용자 및 고지 모델을 보는 것도 도움이됩니다. – pdoherty926

+0

당신은 지금 그것을 가지고 있습니다, pdoherty926 ... –

+1

불행히도, 그것은 우리에게 도움이되는 것을 말하지 않습니다. 사용자 모델은 어떻습니까? 'user # username'은'.name'에 대한 참조를 가지고 있습니까? – pdoherty926

답변

4

오류 역 추적 점을 검색 할 때

어느 당신은 당신이 창조에 기본 국가를 가질 수 있도록, 또는 두 단계에서 그것을 할 블록 내부 :

countries << country_post.country.name 

Undefined method 'name' for nil:NilClass는, nil.name를 호출하려고하는 의미 주어진 country_post, country_post.country 반환 nil을 위해 만든 것입니다.

버그를 해결하려면이 코드 부분을 확인해야합니다. 통지서에

https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L93 https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L62

1

notice.show_countries 메서드를 사용하려는 것 같습니다.

로그에 이라는 메서드가 nil에 존재하지 않는다는 것을 알 수 있습니다. 즉, 개체가 nil 인 것을 의미합니다.

"show_countries"메소드의 내용을 표시하고 name 호출 방법을 담당하는 코드 행에 주어진 정확한 객체가 있는지 확인하십시오.

디버거를 사용해 보셨습니까?

UPDATE :

notice 코드를 기반으로

, 당신의 문제는 일부 게시물을하는 coutry이없는 한 다음, country_post.country이 오류가 발생할 nil 것입니다. 라인 인 notice.rb:7 파일로 정보를

if country_post.country.blank? 
    countries << 'DEFAULT_COUNTRY_NAME' 
else 
    country << country_post.country.name 
2

: country이 nil` 그렇지 않으면 nil, or없는 경우이 try 방법은 country.name를 반환합니다

countries << country_post.country.try(:name) 

: 당신이 할 수있는 또 다른 것은 try 방법을 사용합니다.RB 모델 사용 :

국가 < < country_post.country.try (이름)

1

@mrcasals에 의한 대답이 가장 직접적으로 질문에 응답하고 더 이상 제기되고 그 예외가 발생합니다.

그러나 여기서 "근본적인"문제는 일부 데이터가 데이터베이스에 유지 될 것으로 기대한다는 것입니다. 속성의 존재를 보장하는 문제에 대한 Rails의 정식 해결책은 ActiveRecord 검증을 통하는 것입니다. 일반적으로 overview은 좋은 읽을 거리이며, presence validation은 특히이 문제에 대한 트릭을 수행해야합니다.

+0

'self.country_post'는 ActiveRecord 응용 프로그램의 추가 컨텍스트없이 데이터베이스에 유지 된 개체. 그래서 검증에 관해서 이야기하지는 않았지만 바로 여기 있습니다. – mrcasals

관련 문제