Rails 3.1 with jQuery-File-Upload和嵌套的Paperclip

时间:2011-10-30 15:22:13

标签: jquery ruby-on-rails-3.1 paperclip

我正在使用带有Paperclip的Rails 3.1并寻找实现jQuery-File-Upload的方法。

我按照例子进行了基本的工作设置:
blueimp jquery=file-upload

但是当我介绍一个嵌套属性模型,其中上传嵌套在post中时,它就会崩溃。给出的错误是“未知属性:图片”。了解uploads_attributes可能必须单独编组,特别是对于图片参数,从查询脚本到控制器。我该如何指定参数?

请求:http://localhost:3002/posts/3

错误:

ActiveRecord::UnknownAttributeError in PostsController#update

unknown attribute: picture

Rails.root: /Users/mjwong/workspace/testupload
Application Trace | Framework Trace | Full Trace

app/controllers/posts_controller.rb:63:in `block in update'
app/controllers/posts_controller.rb:62:in `update'

请求

参数:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"1SYnIX+Y5PxM7PskzV/zocCJfh60tyGkUBdwu7OmNo8=",
 "post"=>{"title"=>"test",
 "content"=>"test",
 "picture"=>#<ActionDispatch::Http::UploadedFile:0x007fe346dbeaf8 @original_filename="Alva 2.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"post[picture]\"; filename=\"Alva 2.jpg\"\r\nContent-Type: image/jpeg\r\n",
 @tempfile=#<File:/var/folders/_w/24l2jy1563d2qqh8w0x4qmgh0000gn/T/RackMultipart20111030-3671-10zn91c>>},
 "commit"=>"Update Post",
 "id"=>"3"}

型号:

class Post < ActiveRecord::Base
   has_many :uploads, :dependent => :destroy
   accepts_nested_attributes_for :uploads, :allow_destroy => true
end

class Upload < ActiveRecord::Base
   belongs_to :post
   has_attached_file :picture, :styles => {:thumb   => "100x100#",
                                           :small   => "300x300>",
                                           :large   => "600x600>" },
   :url => "/system/:class/:attachment/:id/:style/:basename.:extension",
   :path => ":rails_root/public/system/:class/:attachment/:id/:style/:basename.:extension"

   validates_attachment_size :picture, :less_than => 500.kilobytes
   validates_attachment_content_type :picture, :content_type => ['image/jpeg', 'image/png']
end

控制器:

class HomeController < ApplicationController
   def index
      @upload  = Upload.new
   end
end

1 个答案:

答案 0 :(得分:0)

忘记运行rails g paperclip posts picture之类的迁移并运行rake db:migrate