2013-06-18 74 views
1

我創建了一個表格,以上傳圖片的一面旗幟文件,這是在我的控制器代碼:無法轉換ActionDispatch :: HTTP :: UploadedFile的轉換成String,而在上傳軌道

def banner_editprocess 
    uploaded_io = params[:banner] 
    if uploaded_io != nil && uploaded_io != "" 
     File.open(Rails.root.join('app','assets','images','banner', uploaded_io.original_filename), 'wb') do |file| 
     file.write(uploaded_io.read) 
    end 
    end 
    r = Banner.find(params[:id]) 
    params.each do |k, v| 
    r[k] = v 
    end 
    if uploaded_io != nil && uploaded_io != "" 
    if r.banner != uploaded_io.original_filename 
     if r.banner != "" && r.banner != nil 
     File.delete(Rails.root.join('app','assets','images','banner', r.banner)) 
     end 
    end 
    r.banner = uploaded_io.original_filename 
    end 

,這是我的看法:

= form_tag("/cms/servicepages/banner-edit-process", :multipart => true) do 
%input{ :type => "hidden", :name => "id", :value => "#{@t.id}" } 
    %table 

    %tr 
     %th Banner Background  
    %td 
     %input{ :type => "file", :name => "banner", :value => "#{@t.banner}"} 

當我嘗試並上傳圖片我得到這個錯誤:

can't convert ActionDispatch::Http::UploadedFile into String 

回答

0

我猜測它的HA因爲你正在嘗試對字符串測試uploaded_io,所以你最好做類似

uploaded_io = params[:banner] 
if uploaded_io.is_a?(ActionDispatch::Http::UploadedFile) 
    # file processing logic 
end 
相關問題