nginx上传模块

时间:2010-12-02 17:51:02

标签: ruby-on-rails-3 nginx

我在使用我的rails应用程序使用nginx上传模块时遇到了一些麻烦。

我的路线

     match '/images/fast_upload' => 'images#create', :via => :post

图像模型

   attr_accessor :tmp_upload_dir
   after_create  :clean_tmp_upload_dir


   # handle new param
    def fast_asset=(file)
      if file && file.respond_to?('[]')
      self.tmp_upload_dir = "#{file['filepath']}_1"
      tmp_file_path = "#{self.tmp_upload_dir}/#{file['original_name']}"
      FileUtils.mkdir_p(self.tmp_upload_dir)
      FileUtils.mv(file['filepath'], tmp_file_path)
      self.asset = File.new(tmp_file_path)
      end
   end    

 private
 # clean tmp directory used in handling new param
  def clean_tmp_upload_dir
   FileUtils.rm_r(tmp_upload_dir) if self.tmp_upload_dir && File.directory?  (self.tmp_upload_dir)
  end        

nginx.conf

  upload_pass @fast_upload_endpoint;


  upload_store /pathto/shared/uploads_tmp 1;


  upload_store_access user:rw group:rw all:r;


  upload_set_form_field upload[fast_asset][original_name] "$upload_file_name";
  upload_set_form_field upload[fast_asset][content_type] "$upload_content_type";
  upload_set_form_field upload[fast_asset][filepath] "$upload_tmp_path";

  upload_pass_form_field "^image_id$|^authenticity_token$|^format$";
  upload_cleanup 400 404 499 500-505;
  }

  location @fast_upload_endpoint {

     passenger_enabled on;
     rails_env production;
  }

  location / {
   rails_env production;
   passenger_enabled on;
  }

在控制器中我的创建方法

    def create
     @image = current_user.images.build(params[:image])
     if @image.save

基本上我不知道如何使用这个创建方法来使用nginx上传。我试图使用@image = @ resource.current_user.images.build(params [:image]),但这给了我一个未定义的方法错误。

1 个答案:

答案 0 :(得分:1)

您应该检查的是创建上传时nginx传递的参数。我和你一样有着相同的逻辑。我得到的创建动作如下。忍受我,因为我现在无法测试nginx通过的参数。但我认为这不是“形象”而是“上传”

@photo = @artist.photos.build(params[:upload])

是我的创建方法。