Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Doesn't Look In Public Folder For Assets On Show Method

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.

like image 544
Noz Avatar asked Jun 26 '12 20:06

Noz


2 Answers

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...

like image 135
Noz Avatar answered Oct 23 '22 23:10

Noz


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

like image 38
ScottJShea Avatar answered Oct 23 '22 23:10

ScottJShea