# Images are not shown on server / incorrect path

**URL:** <https://www.ruby-forum.com/t/images-are-not-shown-on-server-incorrect-path/149580>\
**Category:** Rails\
**Created:** [September 26, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/images-are-not-shown-on-server-incorrect-path/149580 "2008-09-26T14:21:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefankroes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/77aa72/32.png) [@stefankroes](https://www.ruby-forum.com/u/stefankroes)\
**Post date:** [September 26, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/images-are-not-shown-on-server-incorrect-path/149580/1 "2008-09-26T14:21:59Z")

</div>

Hi folks,

i’m new to ROR. My Application works fine on my local machine, but if i  
deploy my application the pictures and files are not shown correctly.  
Instead of taking the right uri  
“[http://adress/employee\_public/pictures/0000/0010/not.gif](http://adress/employee_public/pictures/0000/0010/not.gif)” it takes the  
uri “[http://adress/pictures/0000/0010/not.gif](http://adress/pictures/0000/0010/not.gif)” like on my local machine,  
where the paths looks like this:  
[http://localhost:3000/pictures/0000/0010/not.gif](http://localhost:3000/pictures/0000/0010/not.gif)

I use attachment\_fu for handling uploads.

Everything in the application works fine, except the images and files.  
Where is the problem. My server runs Apache/2.2.3 (Debian) mod\_jk/1.2.26  
Phusion\_Passenger/2.0.3 PHP/4.4.4-8+etch6 mod\_ssl/2.2.3 OpenSSL/0.9.8c

My file\_system\_backend.rb:

1. require ‘ftools’
2. module Technoweenie # :nodoc:
3. module AttachmentFu # :nodoc:
4. 

```
module Backends

```

5. 

```
  # Methods for file system backed attachments

```

6. 

```
  module FileSystemBackend

```

7. 

```
    def self.included(base) #:nodoc:

```

8. 

```
      base.before_update :rename_file

```

9. 

```
    end

```

10. 
11. 

```
    # Gets the full path to the filename in this format:

```

12. 

```
    #

```

13. 

```
    # # This assumes a model name like MyModel

```

14. 

```
    # # public/#{table_name} is the default filesystem path

```

15. 

```
    # RAILS_ROOT/public/my_models/5/blah.jpg

```

16. 

```
    #

```

17. 

```
    # Overwrite this method in your model to customize the

```

filename.  
18. # The optional thumbnail argument will output the  
thumbnail’s filename.  
19. def full\_filename(thumbnail = nil)  
20. file\_system\_path = (thumbnail ? thumbnail\_class :  
self).attachment\_options[:path\_prefix].to\_s  
21. File.join(RAILS\_ROOT, file\_system\_path,  
\*partitioned\_path(thumbnail\_name\_for(thumbnail)))  
22. end  
23.  
24. # Used as the base path that #public\_filename strips off  
full\_filename to create the public path  
25. def base\_path  
26. @base\_path ||= File.join(RAILS\_ROOT, ‘public’)  
27. end  
28.  
29. # The attachment ID used in the full path of a file  
30. def attachment\_path\_id  
31. ((respond\_to?(:parent\_id) && parent\_id) || id).to\_i  
32. end  
33.  
34. # overrwrite this to do your own app-specific  
partitioning.  
35. # you can thank Jamis B. for this:  
[http://www.37signals.com/svn/archives2/id\_partitioning.php](http://www.37signals.com/svn/archives2/id_partitioning.php)  
36. def partitioned\_path(\*args)  
37. (“%08d” % attachment\_path\_id).scan(/…/) + args  
38. end  
39.  
40. # Gets the public path to the file  
41. # The optional thumbnail argument will output the  
thumbnail’s filename.  
42. def public\_filename(thumbnail = nil)  
43. full\_filename(thumbnail).gsub  
%r(^#{Regexp.escape(base\_path)}), ‘’  
44. end  
45.  
46. def filename=(value)  
47. @old\_filename = full\_filename unless filename.nil? ||  
@old\_filename  
48. write\_attribute :filename, sanitize\_filename(value)  
49. end  
50.  
51. # Creates a temp file from the currently saved file.  
52. def create\_temp\_file  
53. copy\_to\_temp\_file full\_filename  
54. end  
55.  
56. protected  
57. # Destroys the file. Called in the after\_destroy  
callback  
58. def destroy\_file  
59. FileUtils.rm full\_filename  
60. # remove directory also if it is now empty  
61. Dir.rmdir(File.dirname(full\_filename)) if  
(Dir.entries(File.dirname(full\_filename))-[‘.’,‘…’]).empty?  
62. rescue  
63. logger.info “Exception destroying  
#{full\_filename.inspect}: [#{$!.class.name}] #{$1.to\_s}”  
64. logger.warn $!.backtrace.collect { |b| " \> #{b}"  
}.join(“\n”)  
65. end  
66.  
67. # Renames the given file before saving  
68. def rename\_file  
69. return unless @old\_filename && @old\_filename !=  
full\_filename  
70. if save\_attachment? && File.exists?(@old\_filename)  
71. FileUtils.rm @old\_filename  
72. elsif File.exists?(@old\_filename)  
73. FileUtils.mv @old\_filename, full\_filename  
74. end  
75. @old\_filename = nil  
76. true  
77. end  
78.  
79. # Saves the file to the file system  
80. def save\_to\_storage  
81. if save\_attachment?  
82. # TODO: This overwrites the file if it exists, maybe  
have an allow\_overwrite option?  
83. FileUtils.mkdir\_p(File.dirname(full\_filename))  
84. File.cp(temp\_path, full\_filename)  
85. File.chmod(attachment\_options[:chmod] || 0644,  
full\_filename)  
86. end  
87. @old\_filename = nil  
88. true  
89. end  
90.  
91. def current\_data  
92. File.file?(full\_filename) ? File.read(full\_filename) :  
nil  
93. end  
94. end  
95. end  
96. end  
97. end

And my helper function for showing the pictures looks like this:

1. def picture\_for(employee, display\_height = nil, img\_size = :small)
2. 

```
if employee.picture

```

3. 

```
  picture_image = employee.picture.public_filename(img_size)

```

4. 

```
  link_to(image_tag(picture_image, :height => display_height),

```

employee.picture.public\_filename)  
5. else  
6. image\_tag(“employee\_portrait.gif”, :height =\>  
display\_height)  
7. end  
8. end

If you need more infos, write me please. I’ll appreciate any help.
