如何在不使用Asset Pipeline的情况下在Rails 5中提供静态图像? [英] How to serve static image in Rails 5 without using Asset Pipeline?

查看:57
本文介绍了如何在不使用Asset Pipeline的情况下在Rails 5中提供静态图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于网络的应用,后端基于 Rails 5。我正在使用 AngularJS 作为前端。我没有使用资产管道来提供任何静态内容,所有脚本(JS& CSS)都加载在位于 index.html 文件中在公共目录中,我从那里使用angular的 ng-route 来进一步管理该位置。

I have a web based app with the back-end build on Rails 5. I am using AngularJS for the front end. I am not using Asset Pipeline to serve any static content, all my scripts(JS&CSS) are loaded in the index.html file located in the public directory and from there I'm using angular's ng-route to further manage the location.

当我的 HTML 页面之一出现问题时,我需要使用 HTML图片标记包含图片,问题是服务器找不到图像文件。

The problem arises when in one of my HTML page I need to include an image using the HTML image tag, the problem is that the server cannot find the image file.

<img src="assets/images/logo.jpg" style="padding-bottom:20px;" />

我试图将图像保存在 app / assets / images public / assets / images 目录但是在所有实例上我收到路由错误,指出我的rails服务器控制台中找不到路由。

I tried keeping my image both in app/assets/images and public/assets/images directory but on all the instances I get routing error stating no route found in my rails server console.

参考几个堆栈溢出答案我在配置development.rb文件中尝试了这一行:

Referring to several stack overflow answers I tried this line in my config development.rb file :

  config.public_file_server.enabled = true

但它不起作用。那么如何在不使用Rails Assetpipeline的情况下提供图像?

But it didn't work. So how can I have served images without using the Rails Assetpipeline?

推荐答案

要在不使用资产管道的情况下提供图像,请放置图像在公共目录中。

To serve images without using the asset pipeline, put your images in the public directory.

your_app / public / images / logo的图片。在您的视图中使用< img src =/ images / logo.jpg 引用jpg

An image at your_app/public/images/logo.jpg is referenced using <img src="/images/logo.jpg in your view.

Rails还为此提供了一个帮助方法,它生成相同的代码,但提供了一些额外的Rails功能:\

Rails also provides a helper method for this, which generates the same code but provides some additional Rails features:\

<%= image_tag('/images/logo.jpg') %>

再次,你省略了 public 部分路径。此外,我建议不要在 public 中设置一个名为 assets - 它会被资产管道覆盖。它也会让你感到困惑。我会把目录名改为别的。

Again, you omit the public part of the path. Furthermore, I'd advise against having a directory in public called assets - that will get overridden by the asset pipeline. It'll also confuse you. I'd change that directory name to something else.

这篇关于如何在不使用Asset Pipeline的情况下在Rails 5中提供静态图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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