【问题标题】:How to use image_url in rails to show images in production?如何在 Rails 中使用 image_url 在生产中显示图像?
【发布时间】:2015-07-05 10:18:35
【问题描述】:

我有这个代码

<div class="w-col w-col-7 w-col-stack w-hidden-small w-hidden-tiny col-spc">
     <div class="nivo-slice" style="left: 0px; width: 900px; height: 100%; opacity: 1;">
          <img src="/assets/home/toppho08.jpg" alt="toppho08.jpg">
     </div>

</div>

它在开发中运行良好,但是当我尝试在生产中运行它时,图像将不会显示。所以,我被告知使用 image_url 但它的 html 似乎不接受代码。我有这个,但一切正常。

<img src="http://sitename.com/assets/home/toppho08.jpg" alt="toppho08.jpg">

但我想使用 image_url。 如何在 html 中应用 image_url

这些链接没有回答我的问题 http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html http://awardwinningfjords.com/2009/05/11/simplify-image-paths-with-image_url.html https://ellislab.com/forums/viewthread/54731/#268424 Rails 4 not right path for image_url

【问题讨论】:

    标签: html ruby-on-rails ruby image url


    【解决方案1】:

    以下就可以使用image_tag

    <%= image_url 'home/toppho08.jpg' %>
    

    【讨论】:

    • 它没用.. 显示的不是照片 >>> localhost:3000/assets/home/…
    • 可能是资产预编译问题,试试RAILS_ENV=production bundle exec rake assets:precompile
    • rake assets:precompile 已经完成,并且在 production.rb 下,我已经将 config.serve_static_assets 和摘要设置为 true。还能是什么?
    • 它必须工作。现在它看起来不像tag 问题。它的一些生产服务器配置问题。您使用的是哪个生产服务器?
    • 到目前为止,我尝试在本地主机生产中运行它只是为了测试它是否可以在服务器上运行
    【解决方案2】:

    只要使用它,它就会为你工作:

    <%= image_tag 'home/toppho08.jpg' %>
    

    <%= image_tag image_url('home/toppho08.jpg') %>
    

    【讨论】:

    • 谢谢,它可以工作...你能帮我解决 CSS 问题吗? .photo-client._4 { background-image: image-tag('home/client4.jpg');这不起作用..图像不会显示..顺便说一下,我的 html 下有这个
    • 使用 image-url 代替 image-tag
    • 背景图片: image-url("home/client4.jpg");
    • 如果您的 css 文件带有 '.css' 扩展名,那么这应该可以工作: background-image: url("/assets/home/client4.jpg")
    • 所以在这种情况下尝试使用'asset-data-url',请检查stackoverflow.com/questions/15257555/…
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签