0

我有一個文件上傳的形式,當提交時,路由到我的parse_upload行動。但是,當我嘗試檢索文件或與文件上傳對象(original_filename,content_type)一起出現的任何屬性時,出現錯誤。如果我理解正確,該文件應存儲在#{Rails.root}/public/uploads under the same name as the original file中。file_field在窗體中不上傳任何東西

當我通過我的應用程序運行時,我得到一個錯誤在我parse_upload行動,表述NoMethodError

NoMethodError in RevenueModelsController#parse_upload 
    undefined method `original_filename' for "rails_upload_test.xlsx":String 

...我上傳的不包含這些方法?文件(和上傳目錄)也不存在。請幫幫忙,我在下面列出了所有相關文件:

的routes.rb

resources :revenue_models do 
    get 'upload', :on => :collection 
end 
match 'revenue_models/upload' => 'revenue_models#parse_upload', :via => :post 
root :to => "home#index" 

控制器操作:

# UPLOAD create instance variable, call onto upload form, and route to parse_upload action 
def upload 
    @uploaded_doc = { :workbook => RubyXL::Parser.new } 
end 
# Parse the uploaded file 
def parse_upload 
    file_name = (params[:uploaded_doc][:workbook]).original_filename 
end 

upload.html.erb - 上傳表單提交到parse_upload行動

<%= form_tag(:url => {:controller => "revenue_models", :action => parse_upload_revenue_models_path}, :html => {:method => "put", :multipart => true}) do %> 
    <%= file_field(:uploaded_doc, :workbook) %> 
    <%= submit_tag("Upload File") %>             
<% end %> 
+0

嘗試將params [:uploaded_doc] [:工作簿]更改爲@uploaded_doc ...或是parse_upload單獨的操作? – 2013-02-23 00:48:29

+0

parse_upload是一個單獨的操作。我的upload.html.erb表單正在發送到parse_upload操作,但沒有變成上傳的文件對象 – Utopia025 2013-02-25 17:47:26

回答

1

form_tag在第一個參數然後選擇一個URL。它看起來像你傳遞一個散列作爲第一個參數,所以我猜你的HTML輸出不是你想要的。嘗試這樣的:

<%= form_tag revenue_models_parse_upload_path, :method => :put, :multipart => true do [...] 
+0

真棒,修復了錯誤 – Utopia025 2013-02-25 20:36:14

相關問題