【问题标题】:RoR3 - image path error?RoR3 - 图像路径错误?
【发布时间】:2011-05-27 12:53:46
【问题描述】:

我在文章 p 标签内的索引页面上显示了一个评论气泡的图像,它显示了添加了多少 cmets。 RoR3 一直报错,让我难住了!?

由于某种原因,它在路径 stylesheets/images/comment.png 中查找 - 当它应该只根据 Ruby 代码块在 /images/comment.png 中查找时。关于我哪里出错的任何建议?

_article.html.erb

<div class="article_header">
<b>Article Title: </b> <%= truncate(article.title, :length => 50) %> 

<div class="com-bub">
<img src="/images/comment.png"/>
</div>

<div class="com-cnt">
<%= article.comments.count %>
</div>

CSS:

.com-bub {
float:right;
background: url('images/comment.png') no-repeat;
margin: -8px 16px 0px 0px;
}

错误:

SQL (0.2ms) SELECT COUNT(*) FROM "articles" WHERE (published = 't') 渲染布局/_footer.html.erb (1.5ms) 渲染布局/_usernav.html.erb (1.3ms) 在布局/应用程序中渲染文章/index.html.erb (190.1ms) 在 289 毫秒内完成 200 次 OK(查看次数:193.7 毫秒 | ActiveRecord:3.6 毫秒)

2011 年 5 月 27 日星期五 13:44:43 +0100 开始为 127.0.0.1 获取“/stylesheets/images/comment.png”

2011 年 5 月 27 日星期五 13:44:43 +0100 开始为 127.0.0.1 获取“/stylesheets/images/comment.png”

2011 年 5 月 27 日星期五 13:44:43 +0100 开始为 127.0.0.1 获取“/stylesheets/images/comment.png”

2011 年 5 月 27 日星期五 13:44:43 +0100 开始为 127.0.0.1 获取“/stylesheets/images/comment.png”

ActionController::RoutingError(没有路由匹配“/stylesheets/images/comment.png”):

渲染 /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.erb 在救援/布局内 (0.7ms)

【问题讨论】:

    标签: css ruby-on-rails-3


    【解决方案1】:

    你的路径不正确,应该是:

    background: url(/images/comment.png) no-repeat;
    

    你需要前导 / 就像在你的标记中一样,你也不需要在你的 css 中打勾。

    【讨论】:

      【解决方案2】:

      使用相对链接:

      background: url(../images/comment.png) no-repeat;
      

      这将考虑到您部署到子 URI 的场景(例如:www.example.org/myapp/)

      【讨论】:

      • 最好避免 .. 原因是,如果您想在 /posts/comments/forum/posts/comments 上使用相同的 css 样式,您正在尝试定义一个级别,它会在第二个失败网址。如果您以 / 开头,您知道它将始终从根开始。
      • 我相信链接是相对于 stylesheet 的位置,而不是控制器。您不能总是假设您将从根目录开始(就像在子 URI 中托管的情况一样)。
      猜你喜欢
      • 1970-01-01
      • 2019-04-19
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多