【问题标题】:rails stripping / inside an SVG image tag轨道剥离/在 SVG 图像标签内
【发布时间】:2021-05-05 13:11:07
【问题描述】:

使用 Rails 6 / Ruby 6:

我正在尝试将图像路径插入 SVG 图像标签,但不知何故路径斜线被剥离了。

<% imgpath = asset_path(challenge.imgname) %>
<%= logger.debug "Asset: " + imgpath %>

输出预期: Asset: /assets/shower.jpg

但在我的 SVG 图像标签内:

<image "<%= asset_path challenge.imgname %>" height="100%" width="100%" preserveAspectRatio="xMidYMid slice" />

(以及在 Chrome 中“检查”时) 它显示为:

<image " assets shower.jpg " height="100%" width="100%" 
 preserveAspectRatio="xMidYMid slice"></image>

使用raw 似乎并没有改变这种行为。

Chrome 中神秘的“查看源代码”确实显示了路径:

<image "/assets/greencommute.jpg" height="100%" width="100%" preserveAspectRatio="xMidYMid slice" />

但不管图像不渲染。

关于发生了什么的任何线索?

提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby svg


    【解决方案1】:

    image 标签需要href 属性:

    <image href="<%= asset_path challenge.imgname %>" height="100%" width="100%" preserveAspectRatio="xMidYMid slice" />
    

    在您当前的代码中呈现的 url 被浏览器解析为属性名称(不仅是浏览器 - 请注意代码突出显示颜色)

    【讨论】:

    • 感谢 Vasfed - 我知道这一定是愚蠢的;就在那里!当我从静态内容转到动态内容时,我丢失了 href。
    猜你喜欢
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2019-11-21
    • 2011-04-02
    • 1970-01-01
    • 2015-10-22
    相关资源
    最近更新 更多