2014-09-25 2 views
2

이미지가 & 개가 업로드 될 수있는 Post 모델이 있습니다. Carrierwave의 두 명의 다른 업 로더.모델에 Carrierwave가있는 사진이 있는지 확인하려면 어떻게합니까?

내가하고 싶은 일은 사진에 업로드 된 사진이 있는지 확인하고, 발견되면 이미지 태그를 표시합니다. 그러나 그렇지 않다면 아무 것도 보여주지 않습니다.

내가이 시도 :

<img class="entry-avatar" height="52" src width="52"> 

가 비어 있지 않은, 그것은에 올바르게, 파일 경로를 추가합니다

<% if post.photo %> 
    <%= image_tag post.photo, size: "52", :class => 'entry-avatar' %> 
<% end %> 

하지만 그건 작동하지 않습니다, 그것은 항상 보여줍니다 src 특성 내가 photo.nil?를 확인하려고하면

그것은 보인다하더라도, 작동하지 않습니다 같은 그것을해야한다 :

> a 
=> #<Post id: 1, title: "Fire at Bible College in Christian", photo: nil, body: "A massive fire is now raging at the Bible College ...", created_at: "2014-08-28 08:06:19", updated_at: "2014-09-18 23:35:04", user_id: 1, ancestry: nil, file: nil, status: nil, slug: "fire-at-bible-college-in-christian", publication_status: 1, has_eyewitness: true> 

:photo 속성이 위 nil로 설정 ... 아직 것을 알 나는이 작업을 수행 할 때 :

> a.photo 
=> #<PhotoUploader:0x00000102e19878 @model=#<Post id: 1, title: "Fire at Bible College in Christian", photo: nil, body: "A massive fire is now raging at the Bible College ...", created_at: "2014-08-28 08:06:19", updated_at: "2014-09-18 23:35:04", user_id: 1, ancestry: nil, file: nil, status: nil, slug: "fire-at-bible-college-in-christian", publication_status: 1, has_eyewitness: true>, @mounted_as=:photo> 
> a.photo.nil? 
=> false 

작동하지 않습니다.

이 방법이나 다른 방법으로 사용할 수있는 Carrierwave 방법이 있습니까?

+1

일단 이것을 확인하고 출력 내용을 확인하십시오 :'a.photo?' – anusha

답변

3

photo 속성을 nil? 메서드로 검사 할 필요가 없습니다. a.photo?과 같이 직접 확인할 수 있습니다. 파일이 존재하는지 여부에 따라 부울 값을 반환합니다.

관련 문제