我有一个文件上传表单,提交后会路由到我的 parse_upload 操作。但是,当我尝试检索文件或文件上传对象附带的任何属性(original_filename、content_type)时,我收到错误消息。如果我理解正确,该文件应该以与原始文件相同的名称存储在 #{Rails.root}/public/uploads 中。
当我运行我的应用程序时,我的 parse_upload 操作中出现错误,指出 NoMethodError
NoMethodError in RevenueModelsController#parse_upload
undefined method `original_filename' for "rails_upload_test.xlsx":String
...我的上传不包含这些方法?该文件(和上传目录)也不存在。请帮忙,我在下面列出了所有相关文件:
路线.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 %>