2011-01-07 90 views
0

我有一段時間從我的POST數組中抓取一個參數並將其插入MySQL數據庫。該文件被提交給RAILS 3控制器,該控制器應該提取文件名並將其用作SQL語句中的值。這是我的控制器:Ajax上傳POST數據沒有插入數據庫在Rails 3

def upload 
    @item= Item.new(params[:qqfile]) 
    Rails.logger.info("PARAMS: #{params.inspect}") 
    # if params array contains :qqfile 
    unless params[:qqfile].empty? 
     # model should: 
     # copy file to uploads directory 
     render_success 
     if @item.save 

     else 
     render :text => 'database did not save' 
     end 
     # redirect to render success action 
    else 
     render :text => '{success:false}' 
    end 
    end 

日誌顯示,查詢運行,但有一個NULL值:

Started POST "/item/upload?qqfile=1eCH6.jpg" for 127.0.0.1 at Fri Jan 07 09:16:0 
9 -0700 2011 
    Processing by ItemController#upload as HTML 
    Parameters: {"qqfile"=>"1eCH6.jpg"} 
PARAMS: {"qqfile"=>"1eCH6.jpg", "controller"=>"item", "action"=>"upload"} 
Rendered text template (0.0ms) 
    SQL (0.0ms) INSERT INTO `items` (`name`, `qqfile`, `created_at`, `updated_at` 
) VALUES (NULL, NULL, '2011-01-07 16:16:10', '2011-01-07 16:16:10') 
Completed 200 OK in 359ms (Views: 16.0ms | ActiveRecord: 0.0ms) 

的關鍵是有在數組中。我很困惑爲什麼控制器沒有使用它的值作爲SQL查詢中的值。

回答

0

我強烈建議使用paperclip進行文件上傳。處理上傳可能會很痛苦,回形針使它更容易。

+0

在這種情況下,這是在提交實際表單之前發生的ajax上載。當通用文件上傳時,回形針是非常棒的......但是當你用進度條做AJAX時,回形針並不是那麼棒。 – 2011-01-21 14:28:03