6

In my show views whenever I try and display an image using the image_tag builder rails doesn't look for images in the public folder inside of my show views...

For instance:

<%= image_tag "thumbnails/fish.jpg" %>

Will produce this:

ActionController::RoutingError (No route matches [GET] "/uploads/thumbnails/fish.jpg"):

I'm using the paperclip Gem for my upload model and I'm saving uploads to a different folder than the public folder for security reasons, and yes, this show view does occur within the Upload controller...

In my Upload model I use this line to save uploads to a non-public folder:

has_attached_file :upload,  :path => ":rails_root/:class/:id/:basename.:extension",
                            :url => ":rails_root/:class  /:id/:basename.:extension"

Rake routes:

upload GET    /uploads/:id(.:format)                                                                 {:action=>"show", :controller=>"uploads"}
       PUT    /uploads/:id(.:format)                                                                 {:action=>"update", :controller=>"uploads"}
       DELETE /uploads/:id(.:format)                                                                 {:action=>"destroy", :controller=>"uploads"}
              /download/:id(.:format)                                                                {:controller=>"uploads", :action=>"download"}

Edit Note: If I explicitly make an img tag and point the src to my image it works fine on my show views so I don't think it's a permission issue.

Noz
  • 6,216
  • 3
  • 47
  • 82

2 Answers2

7

The answer was fairly simple, and I can't believe Rails is being this picky but I needed to include a forward slash at the beginning of the path as so:

"thumbnails/fish.jpg"

becomes

"/thumbnails/fish.jpg"

I'm still curious as to why this is only a problem on non-index views...

Noz
  • 6,216
  • 3
  • 47
  • 82
  • By non-index views, did you mean those that are not `http://example.com/` , but `http://example.com/someview` or `http://example.com/anotherview` ? If yes, then the difference is: having a leading slash defines an absolute path from your index view; without the leading slash, it's a relative path, meaning you're accessing `/someview/thumbnails/fish.jpg`, which is the wrong path. – Nikki Erwin Ramirez Jul 05 '12 at 10:20
  • I meant literally the index route. Evidently there's a difference between `GET /model ` and `GET /model/id` when serving assets. Either that or there's a problem with my routes. – Noz Jul 05 '12 at 15:50
2

You may need to change this line to true:

config.serve_static_assets = false

in the environment/*.rb file (e.g. development.rb).

Or I have seen where this is a permissions issue on the directory in question so a CHMOD 777 on the directory may resolve it/point you in the right direction.

Here is an SO ticket on a similar issue

Community
  • 1
  • 1
ScottJShea
  • 7,041
  • 11
  • 44
  • 67