2010-12-02 107 views
0

我在使用我的rails應用程序獲取nginx上傳模塊時遇到了一些麻煩。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

你應該檢查什麼是創建上傳時nginx傳遞的參數。我有和你一樣的邏輯。我得到的創建操作如下。忍受我,因爲我無法現在測試參數nginx通行證。但我認爲這不是「圖像」,而是「上傳」

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

是我的創建方法。