0
我使用Rails 3.1與Paperclip和尋找一種方法來實現jQuery的文件上傳。
Rails 3.1與jQuery的文件上傳和嵌套的回形針
我通過下面的例子已經基本工作設置:
blueimp jquery=file-upload
但是,當我介紹這裏上傳嵌套帖子內嵌套的屬性模型,它分崩離析。給出的錯誤是「未知屬性:圖片」。瞭解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
喜邁克爾,是這仍然是一個問題?視圖是什麼樣的? – brookr