Rails 图像断开的链接 [英] Rails image broken link

查看:35
本文介绍了Rails 图像断开的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试链接到 rails 中的图像,但无法正常工作.我可以链接到 assets/file.css 以及 assets/file.js 但 assets/file.jpg 不起作用.

我尝试了一个新的 rails new project_name,当我打开主页时,rails 标志没有显示.

作为一个附带问题,

如何让 rails 只加载 application.js 和 application.css

我看到,如果我从 URL 中删除 ?body=1,它会将它们全部压缩到一个文件中,但是当我使用以下代码查看页面时,它会将这些文件留空,并将它们作为单独的脚本一一包含.

 <%= stylesheet_link_tag "application", :media =>全部"%><%= javascript_include_tag "应用程序" %>

解决方案

好的,这是预期的行为,您必须使用 Rails 助手:

 <%= image_tag "file.jpg" %>

您的 css 文件应该是 .erb.在其中,使用:

.class { background-image: url(<%= asset_path 'image.png' %>) }

参考第 2.2.1 段此处.>

I'm trying to link to an image in rails and its not working. I can link to assets/file.css as well as assets/file.js but assets/file.jpg doesnt work.

I tried a fresh rails new project_name and when i open the homepage and the rails logo doesnt show.

As a side question,

How do I get rails to load only the application.js and application.css

I see that if i remove ?body=1 from the URL it compresses them all into one file, but when I view the page using the following code it leaves those files blank and includes them one by one as separate scripts.

  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>

解决方案

Ok so that's an expected behavior, you have to use Rails helpers:

 <%= image_tag "file.jpg" %>

Your css files should be .erb. Within it, use:

.class { background-image: url(<%= asset_path 'image.png' %>) }

Reference is para 2.2.1 here.

这篇关于Rails 图像断开的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆