Paras
Paras

Reputation: 3481

How to serve static image in Rails 5 without using Asset Pipeline?

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.

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;" />

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.

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

  config.public_file_server.enabled = true

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

Upvotes: 4

Views: 5412

Answers (1)

stef
stef

Reputation: 14268

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

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

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

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

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.

Upvotes: 5

Related Questions