How to check if image exists in Rails?

前端 未结 4 1849
再見小時候
再見小時候 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:24

    You can use File.file? method.

    if File.file?("#{Rails.root}/app/assets/images/{image_name}")
      image_tag("#{image_name}")
    end
    

    You can also use File.exist? method but it will return true if it finds a directory or a file. The method file? is slightly more picky than exist?

提交回复
热议问题