1

这是我的模型

class Upload < ActiveRecord::Base
  belongs_to :post
  has_attached_file :upload,styles: { medium: ["500x300>",:jpg], thumb: ["150x100#",:jpg] }, default_url: '/assets/avatar.jpg', url: "/post_images/post_:post_id/:style/:filename"


  def post_id
    self.post.id
  end

end

这是yui的我的上传器js

var post_id = Y.one('#post_id').get('value');
        var token = Y.one('meta[name="csrf-token"]').get('content');
        var uploader = new Y.Uploader({width: "250px",
                                      height: "35px",
                                      multipleFiles: true,
                                      swfURL: "http://yui.yahooapis.com/3.10.1/build/uploader/assets/flashuploader.swf?t=" + Math.random(),
                                      uploadURL: "/uploads/post_image.json?post_id="+post_id+"&authenticity_token="+token,
                                      simLimit: 2,
                                      withCredentials: false,
                                      uploadHeaders: {'X-CSRF-Token': token}
                                     });
       var uploadDone = false;

这是我的 uploads_controller.rb

  def post_image
    post_id = params[:post_id]
    @images = Upload.new(post_id: post_id)
    @images.save!

    respond_to do |format|
      format.html { render nothing: true}
      format.json { render json: @images.post_id}
    end
  end

这是我用于上传的数据库架构

 create_table "uploads", force: true do |t|
    t.integer  "post_id"
    t.string   "upload_file_name"
    t.string   "upload_content_type"
    t.integer  "upload_file_size"
    t.datetime "upload_updated_at"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

发生的事情是如果我上传执行以下查询

INSERT INTO "uploads" ("created_at", "post_id", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", Sat, 30 Nov 2013 16:14:36 UTC +00:00], ["post_id", 336], ["updated_at", Sat, 30 Nov 2013 16:14:36 UTC +00:00]]

图像未上传,目录也未创建。而且在上面的查询中也没有为以下列插入值upload_file_name,upload_content_type,upload_file_size,upload_updated_at

谁能帮我。

4

0 回答 0