【问题标题】:Rails and SLIM html image_tag points to images instead of assetsRails 和 SLIM html image_tag 指向图像而不是资产
【发布时间】:2018-10-24 07:01:12
【问题描述】:

我有一个场景,我需要使用 SLIM 编写的模板发送电子邮件。我用我认为必要的config 修复了我的environment/development.rb(也基于我的研究)

config.assets.precompile += %w( print.css )
config.serve_static_assets = true (still using Rails 4.1.16)
config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
config.action_controller.asset_host = 'http://localhost:3000'
config.action_mailer.asset_host = config.action_controller.asset_host

我知道它在此步骤中正确指向了我的根 URL。然后在我的mailer.html.slim 我有这样的东西:

div align="left" style="border-collapse: collapse; padding-left: 30px;padding-top: 30px;padding-bottom: 30px;float:left;"
  a href="http://#{App.url}"
      = image_tag "images/logo-old.png", style: "display:inline-block", border: "0", alt: App.name, width: 139, height: 44, title: App.name

我已经尝试了以下方法:

= image_tag "logo-old.png"
= image_tag "assets/images/logo-old.png"
= image_url "logo-old.png"

没有任何效果,但好在它现在显示 Alt 以了解它仍在按其路径工作。

当前的= image_tag 计算结果为:

 src="https://ci5.googleusercontent.com/proxy/83z0D3PVOqx-FwFFMwztfYBs5CSWHiURyxSUP6cZ3dq7Zo47k9mNPgotrijVmmWGxPHqblRTGCePqN4RrfEhHh_665
MdAQ=s0-d-e1-ft#http://localhost:3000/images/images/logo-old.png"

知道为什么应用找不到正常的管道路径吗? 而不是images/images/logo-old.png 我需要assets/images/logo-old.png

编辑:

所以为了比较结果,如果我做image_url "logo-old.png" 路径结果到/assets/logo-old.png

如果我执行image_url "images/logo-old.png",则路径结果为images/images/logo-old.png

我的图片在app/assets/images/logo-old.png

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 slim-lang


    【解决方案1】:

    1) http://localhost:3000/images/images/logo-old.png 是您的 app/assets/images/images/logo-old.png 的正确文件夹结构吗?

    2) 您是否将app/assets/images 添加到资产管道?

    How to add folders to asset pipeline

    Javascript does not work from a bootstrap template in rails

    3) 您是否检查了Rails.application.config.assets.paths rails console 中的默认搜索路径,您是否尝试正确组织要存储在 app/assets/images 文件夹中的图像并将该文件夹添加到资产管道?

    那你应该可以image_url "logo-old.png"

    谢谢!

    【讨论】:

    • 嗨!感谢您分解我应该仔细检查的内容。重新检查了我的Rails.application.config.assets.path,它确实包含了我需要的项目:"/home/spotontheresa/Documents/projects/myproject/app/assets/images" 据我所知,我的项目结构也正确,它是应用程序-资产-图像-我需要的图像/logo-old.png跨度>
    • 还有什么我应该检查的想法吗?
    • @Theresa 它应该使用来自public/assets 的预编译资产,包括指纹,而它正在回退到您的app/assets/images 文件夹中的资产。可以在 development.rb 内禁用此回退行为 guides.rubyonrails.org/…。事实是您需要清除所有预编译资产并有时进行预编译才能看到更改生效
    猜你喜欢
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 2020-02-27
    • 1970-01-01
    • 2014-08-21
    • 2012-01-28
    相关资源
    最近更新 更多