user984621
user984621

Reputation: 48493

Rails 3 & paperclip - path for storing images

I try to set up the path for storing images with using Paperclip plugin.

In my model I set the path as:

:path => ":rails_root/public/gallery/:user_id/:style/:basename.:extension",
:url => "/gallery/:user_id/:style/:basename.:extension"

But the images are storing to the directory called "/user_id", not to the directory "1" (as the ID of user) - what I am doing wrong? Forgot I on anything important?

EDIT - THE SOLUTION:

If you have a model (eg. photo) and you want save the images to folder that called as ID of another model (eg. user), you have to add to config/initializers the file paperclip.rb with following content:

Paperclip::Attachment.interpolations[:user_id] = proc do |attachment, style|
  attachment.instance.user_id # or whatever you've named your User's login/username/etc. attribute
end

After this update is needed to restart WEBRick yet.

PS - an assumption is the relation between these two models.

Thank you

Upvotes: 1

Views: 1171

Answers (1)

fjsandov
fjsandov

Reputation: 11

There is a new syntax to do this, in case somebody need it:

Paperclip.interpolates :user_id do |attachment, style|
    attachment.instance.user_id
end

Upvotes: 1

Related Questions