views:

306

answers:

3

I am using Paperclip to upload an image to my Project model and I want to have an array of default images (not depending on the style, but different images) is that posible? To pass an array instead of just one URL to the :default_url option?

Thank you,

Nicolás Hock Isaza

A: 

No idea if this will work, but it's worth a try. Put the images 0.png, 1.png, 2.png, 3.png, 4.png on disk, and then in your model:

has_attached_file :image,
  lambda {{
    :default_url => "path/to/images/#{rand(5)}.png"
  }}

Put your other options in the lambda as well.

Ben
I tried to make this work but got `unexpected '\n', expecting tASSOC`. Not sure where the syntax error is.
jaacob
On what line do you get the error?
Ben
A: 

Well I didn't use the lambda function but I got the idea from Ben's answer. I just have the files (0.jgp, 1.jpg ...) and then I can just have

:default_url => "path/to/images/#{rand(5)}.jpg"

With no lambda ;-)

Thank you very much!

Hock
Are the images switching randomly? I would expect that the rand() value would be interpreted only the first time the model is loaded. Try running it in production and seeing if the image switches.
Ben
They don't switch randomly, only on the first time the model is loaded as you expected.
jaacob
+1  A: 

So close: If you want the images to change randomly, and not just on first load of the model:

:default_url => lambda { "path/to/images/#{rand(5)}.jpg" }
brady8