简体   繁体   English

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

[英]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> 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 编辑: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. 似乎您正在尝试测试@images是否有任何图像,并且@images使用了错误的方法。 Instead try: 而是尝试:

- if @images.present?

According to your code, @images is obviously a WillPaginate::Collection of image , which acts like an array. 根据您的代码, @images显然是imageWillPaginate::Collection ,其作用类似于数组。

Therefore, to test whether @images is not empty , you should use if @images.present? 因此,要测试@images是否不为空 ,应该使用if @images.present? or unless @images.empty? unless @images.empty? instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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