Question

I have a file upload form that when submitted, routes to my parse_upload action. However, when I try to retrieve the file, or any of the attributes that come along with the file upload object (original_filename, content_type), I get an error. The file, if I understand correctly should be stored in the #{Rails.root}/public/uploads under the same name as the original file.

When I run through my app, I get an error in my parse_upload action stating a NoMethodError

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

...my upload doesn't contain these methods? The file (and upload directory) also do not exist. Please help, I've listed all relevant files below:

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"

controller actions:

# 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 - upload form submits to the parse_upload action

<%= 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 %> 
Was it helpful?

Solution

form_tag takes a URL in the first argument then options. It looks like you're passing it a hash as the first argument instead so I'm guessing your HTML output is not what you're looking for. Try something like this:

<%= form_tag revenue_models_parse_upload_path, :method => :put, :multipart => true do [...]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top