繁体   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