2013-08-23 2 views
2

루비 버전 NilClass 오류를 던지고 : 2.0 버전는 종이 클립은 좁은 방

레일 : 나는 갤러리 클래스가 4.0

-has_many :assets

을 (자산 Paperclip에서 업로드를 허용하는 모델되는) 것을

색인의 각 갤러리에 대한 축소판 이미지를 표시하려고합니다. 내가 좋아하는 뭔가를하고 싶지 : 그러나

<%= gallery.assets.first.photo.url(:thumb) %>

을, 그 날이 오류를 제공합니다 : 전무에 대한 undefined method 사진 'NilClass`을

여기에 이상한 부분이 작동

가 : 아니 그들 모두 -

<% gallery.assets.each do |asset| %> 
    <%= image_tag asset.photo.url(:thumb) %> 
<% end %> 

는하지만 하나 개의 이미지를 원한다. 내가 뭘 놓치고 있니?

업데이트 여기

요청한 콘솔 출력

Gallery.first.assets

2.0.0p247 :010 > Gallery.first.assets 
    Gallery Load (0.3ms) SELECT "galleries".* FROM "galleries" ORDER BY "galleries"."id" ASC LIMIT 1 
    Asset Load (0.2ms) SELECT "assets".* FROM "assets" WHERE "assets"."gallery_id" = ? [["gallery_id", 2]] 
=> #<ActiveRecord::Associations::CollectionProxy [#<Asset id: 15, gallery_id: 2, created_at: "2013-08-23 23:12:47", updated_at: "2013-08-23 23:12:47", photo_file_name: "mightywash.png", photo_content_type: "image/png", photo_file_size: 24967, photo_updated_at: "2013-08-23 23:12:46">]> 
2.0.0p247 :011 > 

Gallery.first.assets.first

2.0.0p247 :011 > Gallery.first.assets.first 
    Gallery Load (0.4ms) SELECT "galleries".* FROM "galleries" ORDER BY "galleries"."id" ASC LIMIT 1 
    Asset Load (0.2ms) SELECT "assets".* FROM "assets" WHERE "assets"."gallery_id" = ? ORDER BY "assets"."id" ASC LIMIT 1 [["gallery_id", 2]] 
=> #<Asset id: 15, gallery_id: 2, created_at: "2013-08-23 23:12:47", updated_at: "2013-08-23 23:12:47", photo_file_name: "mightywash.png", photo_content_type: "image/png", photo_file_size: 24967, photo_updated_at: "2013-08-23 23:12:46"> 
2.0.0p247 :012 > 
이다,

업데이트 2

asset.rb

class Asset < ActiveRecord::Base 
    belongs_to :gallery 
    has_attached_file :photo, 
     :styles => { 
      :thumb => "100x100#", 
      :small => "300x300>", 
      :large => "600x600>" 
     } 
end 
+1

당신은 우리에게 모두'gallery.assets'and'gallery.assets.first'의 결과를 보여 console''레일을 열 수 있습니까? – Serabe

+0

@Serabe 예 - 지금 업데이트 중입니다. – drewwyatt

+0

@Serabe 업데이트 됨 – drewwyatt

답변

1

나는 당신의 자산 중 하나 의심은 사진이 저장되지 않았을 수 있습니다. 대신 다음을 시도하십시오.

<%= @gallery.assets.first.photo.url(:thumb) if [email protected]? && @gallery.assets.first.photo %> 

또는 이보다 나은 것을 갤러리 모델에 넣으십시오. 보기에

def thumb_url 
    unless assets.empty? 
    assets.first.photo.url(:thumb) if assets.first.photo 
    end 
end 

한 다음 :

<%= @gallery.thumb_url %> 
관련 문제