如果图像不存在则显示文本

时间:2022-11-21 21:12:57

I am trying to set my app up so on the user page, if they don't have any images, then it shows text saying that they don't. It will also show a link to upload new images.

我试图在用户页面上设置我的应用程序,如果他们没有任何图像,那么它会显示文本说他们没有。它还会显示上传新图像的链接。

I have tried this but it shows undefined method 'image?' for #<WillPaginate::Collection:0x007f93a1e0e638>

我试过这个,但它显示未定义的方法'图像?' ##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!

Edit: images_controller.rb

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

2 个解决方案

#1


3  

It looks like you're trying to test whether there are any images in @images, and just using the wrong method for that. Instead try:

看起来你正试图测试@images中是否有任何图像,并且只是使用了错误的方法。而是尝试:

- if @images.present?

#2


0  

According to your code, @images is obviously a WillPaginate::Collection of image, which acts like an array.

根据你的代码,@ images显然是一个WillPaginate ::图像集合,它就像一个数组。

Therefore, to test whether @images is not empty, you should use if @images.present? or unless @images.empty? instead.

因此,要测试@images是否为空,您应该使用if @ images.present?或者除非@ images.empty?代替。

#1


3  

It looks like you're trying to test whether there are any images in @images, and just using the wrong method for that. Instead try:

看起来你正试图测试@images中是否有任何图像,并且只是使用了错误的方法。而是尝试:

- if @images.present?

#2


0  

According to your code, @images is obviously a WillPaginate::Collection of image, which acts like an array.

根据你的代码,@ images显然是一个WillPaginate ::图像集合,它就像一个数组。

Therefore, to test whether @images is not empty, you should use if @images.present? or unless @images.empty? instead.

因此,要测试@images是否为空,您应该使用if @ images.present?或者除非@ images.empty?代替。