我正在使用padrino构建一个站点。我使用paperclip将徽标上传到其中一个模型。我遇到的问题是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。 我还根据Using Paperclip with Padrino将此添加到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
答案 0 :(得分:0)
path: Padrino.root('/public/:attachment/:id/:basename.:extension')
您可以进入padrino console
并尝试:
>> 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!