简体   繁体   中英

Rails if image doesn't exist show text

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> 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!

Edit: images_controller.rb

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

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:

- if @images.present?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM