在rails app中使用paperclip时出错

时间:2015-04-15 10:36:39

标签: ruby-on-rails ruby ruby-on-rails-3 paperclip

目前我尝试用paperclip替换attachment_fu fileuploading插件。

但上传时出现了一些错误:

我的控制器看起来像这样:

def create
add_breadcrumb 'breadcrumb.upload_file', {:action => :new}

puts "BLUBBBBBBBBBBBBBBBBBBBBBBBBBBB"
puts params[:upload]

    @upload = Upload.create(params[:upload])
    @upload.lecturer_id = current_user.id
    if @upload.save
        flash[:notice] = I18n.t("flash.saved")
        redirect_to :action => :index
    else
        render :action => :new
    end
end

我的模特是这样的:

puts has_attached_file :image, :default_url => :file_system
validates_attachment_content_type :image, :content_type => [:image, 'audio/mpeg', 'application/mp3', 'application/octet-stream']

上传时出现此错误:

ActiveRecord::UnknownAttributeError in UploadsController#create

unknown attribute: uploaded_data

app/controllers/uploads_controller.rb:24:in `create'

更新

{"name"=>"TEST", "uploaded_data"=>#<ActionDispatch::Http::UploadedFile:0x00000005518a38 @original_filename="2014-09-26 18.14.22.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[uploaded_data]\"; filename=\"2014-09-26 18.14.22.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20150415-12671-190wpi5>>}

更新:那不是我想要的。迁移generetad数据库中的一些新列,如何返回到previus版本?是否有移民昏迷?

enter image description here

2 个答案:

答案 0 :(得分:1)

您的图片列是您的模型结构中的image。但是图像中的数据来自params[:model_name][uploaded_data]。 您的模型不包含任何名为uploaded_data的列,对于attachment_file

也是如此

form_for中你应该拥有这个

= f.file_field :image

= f.file_field :uploaded_data

通过这种方式你的参数看起来像

{"name"=>"TEST", "image"=>#<ActionDispatch::Http::UploadedFile:0x00000005518a38 @original_filename="2014-09-26 18.14.22.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"upload[uploaded_data]\"; filename=\"2014-09-26 18.14.22.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20150415-12671-190wpi5>>}

它不会给你任何错误

通过github上的gem paperclip

您需要创建这样的迁移文件:

class AddAvatarColumnsToUsers < ActiveRecord::Migration
  def self.up
    add_attachment :model_name, :[image]
  end

  def self.down
    remove_attachment :model_name, :[image]
  end
end

或者使用发电机:

rails generate paperclip model_name [image]

答案 1 :(得分:1)

要从Paperclip恢复迁移,您可以使用:

rake db:migrate:down VERSION=_current_migration_number

rake db:rollback

直到你以前的状态。然后,您可以创建一个新的迁移,如下所示:

class ChangeUploadToPaperclip < ActiveRecord::Migration
  change do
    rename_column :uploads, :content_type, :upload_content_type 
    rename_column :uploads, :filename, :upload_filename
    rename_column :uploads, :size, :upload_file_size
    add_column :uploads, :upload_updated_at, :datetime
    # Warning: the following will remove your old data, irreversible.
    remove_column :uploads, :width 
    remove_column :uploads, :height 
    remove_column :uploads, :thumbnail 
  end
end

但旧存储和Paperclips存储文件的方式之间可能存在潜在错误。

在这个答案中,我假设您的模型has_attached_file :upload

如果您有附加文件的其他名称,请相应地更新每个uploaduploads

修改 迁移文件需要具有某个名称:

20150417125122_change_upload_to_paperclip.rb

(日期字符串是自动生成的,我从下面的评论中复制了它。)

这是因为只有在两者之间存在下划线时,Rails才会查找ChangeUploadToPaperclip

如果您有changeuploadtopaperclip.rb,则会查找Changeuploadtopaperclip

运行特殊迁移

rake db:migrate:up VERSION=20150417125122