1

我正在使用 padrino 建立一个站点。我使用回形针将徽标上传到其中一个模型。我遇到的问题是 Paperclip 不保存附件,但也没有抛出任何错误。我认为传递给控制器​​的参数类型不正确,因为它params[:logo]是一个散列,应该是某种文件类型?我怎样才能让回形针保存参数中传递的附件?

该模型:

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
4

1 回答 1

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!
于 2012-08-07T12:25:12.033 回答