Evgeny Palguev
Evgeny Palguev

Reputation: 609

Rails don't showing images from public folder

I am making an app - it is uploading (with carrierwave) pdf and ppt files, then Docsplit extracts pages/slides to images and save it to public/uploads/document/unique_timestamp/ folder.

In show view the images don't show, but links are correct - for example "http://localhost:3000/Users/my_user_name/Documents/rp/1512/my_app/public/uploads/document/1450527696/qwerty_2.jpg" and I see enter image description here

What can I do?

in controllers/documents_controller.rb:

def show
    @output_path = "#{Rails.root}/public/uploads/document/" + @document.image.to_s[-14,10] + "/"
end

in view/show.html.erb:

<ul>
<% Dir.foreach(@output_path) do |f| %>

  <% if f != "." && f != ".." %>
    <li>
      <%= image_tag @output_path + f %>
      <br><%= f %>
    </li>

  <% end %>

<% end %>
</ul>

Upvotes: 1

Views: 1667

Answers (2)

Enzymz
Enzymz

Reputation: 1

Remove public from your URL and try:

http://localhost:3000/uploads/document/1450527696/qwerty_2.jpg

Upvotes: 0

Holger Just
Holger Just

Reputation: 55908

There is a difference between URL paths and your filesystem paths. Generally, the URL path / is mapped to your public directory on your computer. As such, when requesting http://localhost:3000/uploads/document/1450527696/qwerty_2.jpg, your webserver will serve the file found in /Users/my_user_name/Documents/rp/1512/my_app/public/uploads/document/1450527696/qwerty_2.jpg.

In your code, you have to use the two different paths in their reqpective locations, i.e. the filesystem path when getting the available files and the URL path when generating the image_tag.

Upvotes: 1

Related Questions