简体   繁体   English

为Rails应用创建上传控制器操作

[英]Create an upload controller action for Rails app

I'm making a Rails app, and want to add an upload feature with allows users to upload multiple entries at once through an Excel spreadsheet as opposed to entering one entry at a time. 我正在制作一个Rails应用程序,并希望添加一个上传功能,该功能允许用户通过Excel电子表格一次上传多个条目,而不是一次输入一个条目。

Ideally, I was hoping to add a separate Upload/Submit portion to the bottom of the new.html.erb file (the bold portion being the Upload HTML.erb): 理想情况下,我希望在new.html.erb文件的底部添加一个单独的Upload / Submit部分(粗体部分为Upload HTML.erb):

...
<div class="actions">
    <%= f.submit %>
</div>
<% end %>

**<div class="field">
    Or Upload Multiple Entries <br />        
    <%= form_tag({:action => :upload}, :multipart => true) do %>
        <%= file_field_tag 'multi_doc' %>        
    <% end %>
</div>**

Here is my routes.rb file: 这是我的routes.rb文件:

Dataway::Application.routes.draw do
    devise_for :users

    resources :revenue_models do
      get 'upload', :on => :collection         
    end

    root :to => "home#index"
end

And my revenue_models_controller (haven't developed the action at all yet, now just a redirect): 和我的income_models_controller(还没有开发动作,现在只是重定向):

...
def upload
    redirect_to revenue_models_path
end

I have followed the rails guides for Uploading files as well as for Routing files and I keep getting an error when I attempt to open the /new view I have modified: 我遵循了有关文件上传路由文件的导轨指南,尝试打开已修改的/ new视图时,我始终收到错误消息:

    Routing Error
        No route matches {:action=>"upload", :controller=>"revenue_models"}
    Try running rake routes

When I run rake route, I get an entry for the upload action: 当我运行rake route时,我将获得上载操作的条目:

upload_revenue_models GET    /revenue_models/upload(.:format)   revenue_models#upload

In the end, what I would like to do is upload an excel file with multiple entries, parse it, and conditionally add each data row to my database, which I was under the impression I could specify in the upload action. 最后,我想做的是上传一个具有多个条目的excel文件,对其进行解析,然后有条件地将每个数据行添加到我的数据库中,这给我留下了我可以在上载操作中指定的印象。 Please help! 请帮忙!

Please use form_for instead form_tag . 请使用form_for而不是form_tag

Second What is multi_doc is a field/attribute of revenue_model ?. 其次什么是multi_docfield/attributerevenue_model I guess is a field/attribute of revenue_model . 我想这是revenue_modelfield/attribute

Third, Where is your instance variable revenue_model in the form? 第三,您的实例变量revenue_model的形式在哪里?

Fourth, You have to create first a instance variable of your Model, on your action/controller where you are showing the form, for example @revenue_model = RevenueModel.new . 第四,必须首先在要显示表单的操作/控制器上创建Model的实例变量,例如@revenue_model = RevenueModel.new

After try this code: 尝试以下代码后:

<%= form_for(@revenue_model, :method => :get, :html => {:multipart => true}, :url => { :controller => "revenue_models", :action => "upload"})) do |f| %>
 <%= f.file_field :multi_doc %>
 <%= f.submit :id => "any_id" %>
<% end %>

The question is very confusing, trying to clarify your question specifying the name of your actions and the names of the files in your views. 这个问题非常混乱,试图澄清您的问题,指定您的操作名称和视图中的文件名称。

Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM