How to check if image exists in Rails?

前端 未结 4 1831
再見小時候
再見小時候 2021-01-11 12:39
<%= image_tag(\"/images/users/user_\" + @user_id.to_s + \".png\") %>

How do you check to see if there is such an image, and if not, then disp

4条回答
  •  独厮守ぢ
    2021-01-11 13:16

    You can use File.exist?.

    if FileTest.exist?("#{RAILS_ROOT}/public/images/#{img}")
      image_check = image_tag("#{img}",options)
    else
      image_check = image_tag("products/noimg.gif", options)
    end
    

提交回复
热议问题