1

I am building a site using padrino. I use paperclip for uploading logos to one of the models. The problem I am experiencing is that Paperclip does not save the attachment, but also does not throw any errors. I think that the parameters passed to the controller are of incorrect type, as the params[:logo] is a hash and should probably be a some kind of a file type? How can I make Paperclip save the attachments passed in the parameters?

The model:

class Charity < ActiveRecord::Base
  include Paperclip::Glue

  attr_accessible :name, :description, :logo
  has_attached_file :logo, path: "/public/:attachment/:id/:basename.:extension"
end

The logo is set in the controller like so:

post :create do
  @charity = Charity.new(params[:charity])
  if @charity.save!
    flash[:notice] = 'Charity was successfully created.'
    redirect url(:charities, :edit, id: @charity.id)
  else
    render 'charities/new'
  end
end

The form passing the parameters to the controller looks like this (parts omitted for brevity):

- form_for :charity, url(:charities, :create), multipart: true, class: :form do |f|
  (...)
  .group
    ==f.label :logo
    ==f.error_message_on :logo
    ==f.file_field :logo
  (...)

I am using Paperclip 2.7.0 and Padrino 0.10.7. I also have added this to boot.rb as per Using Paperclip with Padrino :

Padrino.before_load do
  File.send(:include, Paperclip::Upfile)

  Paperclip.options[:logger] = Padrino.logger
  Paperclip.options[:command_path] = "/usr/local/bin"

  ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Paperclip::Schema)
  ActiveRecord::ConnectionAdapters::Table.send(:include, Paperclip::Schema)
  ActiveRecord::ConnectionAdapters::TableDefinition.send(:include, Paperclip::Schema)
end
Community
  • 1
  • 1
nethopp3r
  • 111
  • 4

1 Answers1

0

Mmm, I've no idea. Maybe this:

path: Padrino.root('/public/:attachment/:id/:basename.:extension')

Can you go in padrino console and try:

>> require 'open-uri'
>> puts Paperclip.default_options
>> c = Charity.create!
>> c.logo = open('http://1.bp.blogspot.com/-0Hn3AjTJj6U/TZHe3ragXGI/AAAAAAAAA1M/_SBk3dx61EE/s1600/med_funny-cat.jpg')
>> c.save!
DAddYE
  • 1,719
  • 11
  • 16
  • Tried that, and it does not help because the attachment can be saved if it is a File, but the problem is that in Padrino `params[:logo]` is a hash, while it should be a File. In Rails, the file is passed to the controller as ActionDispatch::HTTP::UploadedFile and Paperclip knows what to do with that, but doesn't know what to do with a hash containing the file details (filename, content_type, etc.) Any way to make Paperclip convert hash -> File? – nethopp3r Aug 07 '12 at 19:19
  • You can use amazon storage instead. Or there is another solution called CarrierWave which is compatible with sinatra. Also you can find rails cast about it. – Ivan Oct 04 '12 at 09:55
  • It's a bit late, but I ended up using CarrierWave and it worked without a problem. Thanks anyway. – nethopp3r Oct 24 '12 at 17:52