0

我有一个文件上传表单,提交后会路由到我的 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 %> 
4

1 回答 1

1

form_tag 在第一个参数中接受一个 URL,然后是选项。看起来您将哈希值作为第一个参数传递给它,所以我猜您的 HTML 输出不是您想要的。尝试这样的事情:

<%= form_tag revenue_models_parse_upload_path, :method => :put, :multipart => true do [...]
于 2013-02-25T19:09:08.537 回答