Using acts_as_api with Paperclip
adampash opened this issue · 4 comments
First, thanks so much for acts_as_api—very useful!
I'm trying to use it with Paperclip, but I'm having some trouble. I have a model with:
has_attached_file :attachment
And I'd like to be able to include :attachment in my acts_as_api template (template.add :attachment)
Attempting to do so like that gives me the following error:
SystemStackError (stack level too deep):
app/controllers/rooms_controller.rb:34:in show' app/controllers/rooms_controller.rb:32:in
show'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (134.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.3ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (154.1ms)
So clearly just adding :attachment to the template and hoping it'll magically work didn't pan out! Any ideas/suggestions for how I might tackle this? With paperclip, I can access different sizes of an uploaded image, for example, using something like @message.attachment(:large) (where @message is an object with has_attached_file and :large is one of the Paperclip-resized versions of the image).
Thanks!
Hi,
you can not simply add the :attachment attribute to the template, because it is a Paperclip::Attachment
object and neither acts_as_api nor the Rails renderers know how to resolve it when rendering the response.
You could do something like
t.add lambda{|model| model.attachment(:large) }, :as => :attachment
Or if you want to return all of the sizes, just create a helper method in your model that constructs a Hash:
def all_avatar_urls
avatars = {}
avatars[:small] = avatar.url(:small)
avatars[:medium] = avatar.url(:medium)
avatars[:large] = avatar.url(:large)
avatars
end
and add it to the template:
t.add :all_avatar_urls
Hope this helps. :)
I'm closing the issue now, but let me know if you need further assistance.
Best,
Chris
edit:
Sorry the first example of the helper method used our privacy gem https://github.com/flinc/concealer
Thanks so much, Christian—that absolutely works. Really appreciate the help!
Best,
Adam
2011/12/1 Christian Bäuerlein <
reply@reply.github.com
Hi,
you can not simply add the :attachment attribute to the template, because
it is aPaperclip::Attachment
object and neither acts_as_api nor the
Rails renderers know how to resolve it when rendering the response.You could do something like
t.add lamda{|model| model.attachment(:large) }, :as => :attachment
Or if you want to return all of the sizes, just create a helper method in
your model that constructs a Hash:def all_avatar_urls avatars = {} avatars[:small] = concealed.avatar.url(:small) avatars[:medium] = concealed.avatar.url(:medium) avatars[:large] = concealed.avatar.url(:large) avatars endand add it to the template:
t.add :all_avatar_urls
Hope this helps. :)
I'm closing the issue now, but let me know if you need further assistance.Best,
Chris
Reply to this email directly or view it on GitHub:
#47 (comment)
I had the same problem and Google brought me here...
it also works for me, thanks too!.
You're welcome :)
Maybe I should extract the relevant content from this ticket into a wiki article....