问题描述
我有一个简单的文件上传表单,用户可以在其中从自己的计算机中选择一个csv文件,然后将其保存到文件夹中。我正在尝试使用Carrierwave,而我的应用程序是在Ruby on Rails中构建的。
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.
当我尝试保存文件时,出现错误没有路由匹配[POST] / customers / new。
When I try to save the file, I get the error "No route matches [POST] "/customers/new".
这里是各种组件。
/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 *
*/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(新方法;我对此没有做任何事情)*
*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 ):
GET /customers index display a list of all photos
GET /customers/new new return an HTML form for creating a new photo
POST /customers create create a new photo
GET /customers/:id show display a specific photo
GET /customers/:id/edit edit return an HTML form for editing a photo
PUT /customers/:id update update a specific photo
DELETE /customers/:id destroy delete a specific photo
如您所见,唯一的POST路径是/ customers。简单调整表格即可解决问题。像下面这样的东西应该起作用:
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:
<%= form_for :dataload, { :url => customers_path , :html => {:multipart => true} } do |f| %>
编辑::根据用户评论添加其他信息。
Adding additional information as a result of user comments.
您还需要向控制器添加一些逻辑,以处理上载的文件。基本上,在Railscast中,他说网站的一部分已经建成:有一个列出画廊的页面,带有每个画廊的链接,和一个显示画廊图片的页面。 。这显然也包括一个新的和编辑页面。来自他的控制器的代码(来自项目源代码-)是:
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:
@gallery = Gallery.new(params[:gallery])
所以,当他添加了文件,并将其作为模型的属性进行了添加,因此他只需添加 attr_accessible:image
位到模型即可使控制器自动开始处理图像(通过相同的参数传递)。由于您正在经历不同的参数(:dataload),而不是作为模型的属性,因此,您可能需要使用当前技术,添加比他更多的代码。也许是这样的:
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 :image
bit 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:
@dataload = Dataload.create(params[:dataload])
甚至:
@dataload = Dataload.process_uploaded_file(params[:dataload])
然后,您将添加一个<$将c $ c> process_uploaded_file 方法添加到 Dataload
来启动相关的客户创建逻辑...
Then, you would add a process_uploaded_file
method to your Dataload
to kick off your relevant Customer creation logic...
还没有真正处理的另一件事是,当数据中有错误时会发生什么。例如,如果上载文件中每10个条目中有2个创建无效记录,您将怎么做(以及如何显示错误)?
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?
这篇关于使用Carrierwave上传文件时,没有路由匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!