2011-12-28 43 views
0

我想在用戶頁面上設置我的應用程序,如果他們沒有任何圖像,那麼它會顯示文本說他們沒有。它還會顯示上傳新圖片的鏈接。Rails如果圖像不存在顯示文本

我都試過,但它顯示undefined method 'image?' for #<WillPaginate::Collection:0x007f93a1e0e638>

- if @images.image? 
     - @images.each do |image| 
      = image_tag ("devices/#{image.device}.png"), :class => "img_#{image.device}" 
      - if image.image? 
       = link_to image_tag(image.image_url(:small), :class => "explore_#{image.device}"), image 
      - else 
       = image_tag '123-small.jpg', :class => "explore_#{image.device}" 
    - else 
     %p Looks like you don't have any images uploaded! 

編輯:images_controller.rb

def index 
    @title = "My Images" 
    @images = current_user.images.paginate(:page => params[:page]) 
end 
+0

可能重複[如何檢查圖像是否存在於Rails?](http://stackoverflow.com/questions/7969241/how-to-check-if-image-exists-in-rails) – 2011-12-28 18:19:41

+0

告訴我們相應的控制器代碼 – 2011-12-28 18:19:43

+0

埃米爾,它不是重複的,因爲它是不同的。 – 2011-12-28 18:20:24

回答

3

它看起來像你想測試是否有任何圖像@images,只是使用錯誤的方法。相反,嘗試:

- if @images.present? 
+0

輝煌。我正在嘗試使用'.exists?'。謝謝 :) – 2011-12-28 18:24:40

0

根據你的代碼,@images顯然是一個imageWillPaginate::Collection,它就像一個數組。

因此,爲了測試是否@images不爲空,你應該使用if @images.present?unless @images.empty?來代替。