# Paperclip generating crap urls on server, works on dev

**URL:** <https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122>\
**Category:** Rails\
**Created:** [August 11, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122 "2010-08-11T18:03:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 11, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/1 "2010-08-11T18:03:32Z")

</div>

Hi,

I have the following:

has\_attached\_file :data,  
:styles =\> {:theora =\> {:format =\> “ogv”}, :still =\> {:format =\>  
“jpg”},  
:small =\> {:format =\> “jpg”}},  
:url =\> “/uploads/products/:basename:suffix.:extension”,  
:path =\> “:rails\_root/uploads/products/:basename:suffix.:extension”

Let’s say I upload a video called video.mp4, now I want paperclip to  
generate the url:

object.data.url(:still) =\> /some/url/video.jpg

But for some reason on my server, paperclip generates:  
/some/url/video.mp4

Moreover, if I update code and restart the server, paperclip will  
correctly generate the .jpg extension the first time, but if I reload  
the page, it messes up and goes back to .mp4.

Everything works fine on my dev machine (mac mongrel+nginx), and this  
strange behavior only happens on the server (freebsd passenger+nginx).

Did anyone experience such weird thing? It’s driving me crazy!.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 11, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/2 "2010-08-11T18:50:13Z")

</div>

Okay so it has to do with caching. On my dev machine, if I switch to  
production environment I get the same behavior. I don’t know what’s the  
solution.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 11, 2010, 11:40pm UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/3 "2010-08-11T23:40:51Z")

</div>

Fernando P. wrote:

> Okay so it has to do with caching. On my dev machine, if I switch to  
> production environment I get the same behavior. I don’t know what’s the  
> solution.

You’re encountering this because Paperclip was originally built to  
process a file into different sizes of itself, original, medium,  
thumbnail etc. In that scenario the format stays the same, so if you use  
a jpg you’ll have a thumbnail jpg as well.

When you’re looking at converting to a different format or, in your  
case, capturing a still it doesn’t check the mime-type for every style  
it has. There is at least one branch on github where somebody has  
attempted to solve this, but here is the approach I’ve followed:

1. Create an initializer at

# config/initializers/paperclip.rb

Paperclip.interpolates :style\_extension do |attachment, style|  
default = File.extname(attachment.original\_filename).gsub(/^.+/, “”)

case style.to\_s  
when /theora/ then ‘ogv’  
when /still/ then ‘jpg’  
else  
default  
end  
end

1. Use that interpolation in your url and path options:  
:url =\> “/uploads/products/:basename:suffix.:style\_extension”,  
:path =\>  
“:rails\_root/uploads/products/:basename:suffix.:style\_extension”

For reference on interpolations:  
[http://rdoc.info/projects/thoughtbot/paperclip](http://rdoc.info/projects/thoughtbot/paperclip)

Hope it helps!

- Parker

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 12, 2010, 11:00am UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/4 "2010-08-12T11:00:50Z")

</div>

Parker S. wrote:

> attempted to solve this, but here is the approach I’ve followed:

Hi Parker, thanks for your message, I was considering doing it that way,  
so I’ll go for it.

Best regards,

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 12, 2010, 11:12am UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/5 "2010-08-12T11:12:25Z")

</div>

> thumbnail etc. In that scenario the format stays the same, so if you use  
> a jpg you’ll have a thumbnail jpg as well.

Still what’s strange is that it should use the :format option I  
provided, that’s what’s written in the docs, so if a jpeg is upload, one  
of its style can be a png. And by looking at its source code that’s what  
expected to occur.

In dev env it works, but once class caching is turned on, it works once,  
then craps out. I’ll see what the devs think about it.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 14, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/6 "2010-08-14T17:57:58Z")

</div>

Ok so there is definitely a bug, thanks for sharing your experience. I  
hope it gets fixed quickly.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 14, 2010, 12:33pm UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/7 "2010-08-14T12:33:56Z")

</div>

We encountered this problem too and decided to dig deeper to fix it.  
You are right about Rails class caching. The Style initializer removes  
the :format, :geometry and :processors options from the options hash  
and stores them as an instance variable. When Rails class caching is  
on, every Style initialized after the first one will not have  
the :format, :geometry and :processors options passed in, because they  
were deleted from the class.

We’ve created an issue on Paperclip’s Github:

> <https://github.com/thoughtbot/paperclip/issues/277>
>
> When Paperclip initializes Paperclip::Style objects, the Style initializer remov…es the :format, :geometry and :processors options from the options hash and stores them as an instance variable. The args hash that is passed in to the Style initializer is a reference to the class inheritable attribute "attachment\_definitions", and these three options are removed from that hash. 
> 
> When Rails class caching is on, every Style initialized after the first one will not have the :format, :geometry and :processors options passed in, because they were deleted from the class. This breaks a lot of things, particularly in our case path and url interpolations for styles with different file extensions.
> 
> We've written some Rails controller tests that demonstrate this. Unfortunately I couldn't write a test in Paperclip itself to show this problem, because there is no Rails environment in the Paperclip test suite:
> 
> \`\`\`
> class Material::Video \< Material
> has\_attached\_file :resource, {
> :styles =\> {
> :Ogg\_preview =\> {:format =\> :ogg, :codec =\> "theora"},
> :H264\_preview =\> {:format =\> :mp4, :codec =\> "h264"}
> },
> :processors =\> \[:video\]
> }
> 
> end
> 
> describe Material::Video do
> describe "attached file" do
> before do
> file = fixture\_file\_upload("files/test.mov", 'video/quicktime')
> @video = MaterialFactory.create\_material({:resource =\> file}, creative\_works(:film1).id)
> @video\_stem = "/system/resources/#{@video.id}"
> end
> 
> it "should have an original version" do
> @video.resource.url.should match "#{@video\_stem}/original/test.mov?"
> end
> 
> it "should create H264 preview (for Safari Users)" do
> @video.resource.url(:H264\_preview).should match "#{@video\_stem}/H264\_preview/test.mp4"
> end
> 
> it "should create Ogg preview (for Firefox Users)" do
> @video.resource.url(:Ogg\_preview).should match "#{@video\_stem}/Ogg\_preview/test.ogg"
> end
> end
> end
> \`\`\`
> 
> The fix is quite simple, just dup the style args when initializing a new Style object (in attachment.rb line 58) so that the options are not deleted from the cached class variable.
> 
> We've forked the repository and made the fix here:
> git://github.com/pivotal-creationmix/paperclip.git

We forked the repository and made the fix here:  
git://github.com/pivotal-creationmix/paperclip.git

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [August 14, 2010, 6:53pm UTC](https://www.ruby-forum.com/t/paperclip-generating-crap-urls-on-server-works-on-dev/193122/8 "2010-08-14T18:53:09Z")

</div>

Nice, thanks for sharing!
