user1011444
user1011444

Reputation: 1399

Image src is not working in Rails

My haml file:

%img{:class => 'thumbnail', :src => '/assets/images/filename.jpg'}

It outputs correctly in the html browser:

<img class="thumbnail" src="/assets/images/filename.jpg">

But the image doesn't show up. This is what Chrome is looking for:

http://127.0.0.1:3000/assets/images/filename.jpg

But when I try to visit that url, I'm being shown:

Routing Error
No route matches [GET] "/assets/images/filename.jpg"
Try running rake routes for more information on available routes.shown

Am I supposed to add something in the config file?? Any help would be much appreciated, thanks.

PS: I am working in development environment, so I serve my own static files.

Upvotes: 0

Views: 2231

Answers (3)

user1011444
user1011444

Reputation: 1399

I solved my issue. Because I am using Rails 3.2 and this version uses the asset pipeline, you have to declare the directories you want to be public. You do this by adding this line in the config/application.rb file:

config.assets.paths << Rails.root.join("path", "to", "dir", "from", "root")

In my case, I wanted the medias/images/thumbnails dir to be part of the assets (medias is at root level), so I achieved this simply by adding:

config.assets.paths << Rails.root.join("medias", "images", "thumbnails")

I can then access them in the templates like other assets with src="/assets/filename.jpg". This is practical because in my case I just want the thumbnails to be public, not the original pictures.

Upvotes: 0

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10198

You should be able to do = image_tag('filename.jpg', class: 'thumbnail') in your haml file.

Also, you can check if there's any image before adding the path by just going to the browser and see if anything comes up, like:

http://127.0.0.1:3000/assets/images/filename.jpg

http://127.0.0.1:3000/assets/filename.jpg

Upvotes: 2

Mikhail Nikalyukin
Mikhail Nikalyukin

Reputation: 11967

You should use /assets/filename.jpg without images namespace.

Upvotes: 8

Related Questions