簡體   English   中英

如果圖像不存在則顯示文本

[英]Rails if image doesn't exist show text

我正在嘗試在用戶頁面上設置我的應用程序,如果他們沒有任何圖像,那么它會顯示文字說明他們沒有。 它還將顯示一個上傳新圖像的鏈接。

我已經嘗試過了,但是它顯示了undefined method 'image?' for #<WillPaginate::Collection:0x007f93a1e0e638> 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

似乎您正在嘗試測試@images是否有任何圖像,並且@images使用了錯誤的方法。 而是嘗試:

- if @images.present?

根據您的代碼, @images顯然是imageWillPaginate::Collection ,其作用類似於數組。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM