2012-03-21 2 views
0

내 AR 모델에 null 값이 기본값 인 텍스트 값이 있습니다. 빈 문자열이 아닌지 여부에 대한 값을 확인하고 싶습니다.AR 모델의 값 - 모든 시나리오를 확인하는 방법

null로 설정하면
ActionView::Template::Error (You have a nil object when you didn't expect it! 
You might have expected an instance of Array. 
The error occurred while evaluating nil.empty?): 

, 속성이 다시 첫 번째 경우에 전무 오류 OUT으로 제공 :

<% if !item.public_notes.empty? && !item.public_notes.nil? %> 
    <%=item.public_notes %> 
<% end %> 

여기에 오류가 있습니다. 나는 이들을 중첩해서는 안된다. 뷰 레이어를 확인하는 적절한 방법은 무엇입니까? 오류가 위에 언급 한 문제를 가리키고 있습니까? (이 전무? 또는 비어 있는지 확인하는?)

들으

답변

2

나는 unless을 사용하고 레일 blank?을 도우미

<% unless item.public_notes.blank? %> 
    <%= item.public_notes %> 
<% end %> 
+0

멋진 들으을 도움. 잘 작동하는 것 같다. – timpone

관련 문제