I have a simple file upload form where a user should be able to select a csv file from their machine and then save it to a file folder. I am trying to user Carrierwave and my app is built in Ruby on Rails.
When I try to save the file, I get the error “No route matches [POST] “/customers/new”.
Here are the various components.
/new.html.erb
<%= form_for :dataload, :html => {:multipart => true} do |f| %>
<p>
<%= f.file_field :file %>
</p>
<p><%= f.submit %></p>
<% end %>
/models/dataload.rb
class Dataload < ActiveRecord::Base
attr_accessible :file_name, :request_user, :source
mount_uploader :file, CustomerWarrantyUploader
end
*/uploaders/customer_warranty_uploader.rb*
class CustomerWarrantyUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(csv)
end
*customers_controller.rb (new method; I haven’t done anything to this)*
def new
@customer = Customer.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @customer }
end
end
Here are the current routes for customers
resources :customers
I am not sure what the issue is, having tried various similar approaches. Any advice is appreciated.
The reason is because that route really doesn’t exist. Resources creates the following routes ( http://guides.rubyonrails.org/routing.html ):
As you can see, the only POST path is /customers. A simple adjustment to your form should do the trick. Something like the following should work:
Edit: Adding additional information as a result of user comments.
You will also need to add some logic to your controller, to deal with the uploaded file. Basically, in the Railscast, he states “Part of the site is already built: there is a page that lists the galleries, with a link to each gallery and a page that shows a gallery’s pictures.”. This apparently includes a new and edit page, as well. The code from his controller ( from the project source code – http://media.railscasts.com/assets/episodes/sources/253-carrierwave-file-uploads.zip ) is:
So, when he added his file, he did it as an attribute on the model, so all he had to add was the
attr_accessible :imagebit to the model to make the controller automagically start handling the image (as it comes through in the same params). As yours is coming through in different params (:dataload), and not as an attribute on the model, you will likely need to add more code than he has, using your current technique. Maybe something along the lines of:or even:
Then, you would add a
process_uploaded_filemethod to yourDataloadto kick off your relevant Customer creation logic…Another thing not really handled, yet, is what will happen when there are errors in the data. For example, what will you do (and how will you present the errors) if 2 out of 10 entries in the uploaded file create invalid records?