padrinoを使ってサイトを構築しています。モデルの 1 つにロゴをアップロードするには、ペーパークリップを使用します。私が経験している問題は、Paperclip が添付ファイルを保存しないだけでなく、エラーもスローしないことです。コントローラーに渡されたパラメーターの型が間違っていると思います。これparams[:logo]
はハッシュであり、おそらくある種のファイル型である必要がありますか? パラメータで渡された添付ファイルを Paperclip に保存させるにはどうすればよいですか?
モデル:
class Charity < ActiveRecord::Base
include Paperclip::Glue
attr_accessible :name, :description, :logo
has_attached_file :logo, path: "/public/:attachment/:id/:basename.:extension"
end
ロゴは次のようにコントローラーに設定されます。
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
パラメーターをコントローラーに渡すフォームは次のようになります (簡潔にするために一部を省略しています)。
- 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
(...)
Paperclip 2.7.0 と Padrino 0.10.7 を使用しています。また、パドリーノでのペーパークリップの使用に従って、これを boot.rb に追加しました。
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