【问题标题】:Ruby/Sinatra/erb: 404 for loading imageRuby/Sinatra/erb:404 用于加载图像
【发布时间】:2013-11-13 12:06:20
【问题描述】:

我尝试显示图像,但日志中的响应代码为 404:

0:0:0:0:0:0:0:1 - - [02/Nov/2013 11:23:55] "GET /public/images/gtk_refresh.png HTTP/1.1" 404 466 0.0030
0:0:0:0:0:0:0:1 - - [02/Nov/2013:11:23:55 MEZ] "GET /public/images/gtk_refresh.png HTTP/1.1" 404 466
http://localhost:4567/main-> /public/images/gtk_refresh.png

rb 文件:

get '/main' do
  erb: main         
end

erb 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1

/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>hello world</title>
</head>

<body>    
    <img src="public/images/gtk_refresh.png" />     
</body>
</html>

我试过图片的相对路径和绝对路径,有点不行。

【问题讨论】:

    标签: ruby image sinatra erb


    【解决方案1】:

    变化:

    <img src="public/images/gtk_refresh.png" />
    

    到:

    <img src="/images/gtk_refresh.png" />  
    

    【讨论】:

    • 我遇到了类似的问题,但即使将图像 src 称为 /images/image.png 也会导致 404。我还需要做其他设置吗?
    【解决方案2】:

    你也可以使用 url helper。

    <img src="<%= url('/images/gtk_refresh.png') %>" />
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 2015-12-06
      • 2015-09-10
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2018-01-26
      • 2011-08-10
      • 1970-01-01
      相关资源
      最近更新 更多