简体   繁体   English

Rails管理员 - 上传CSV以创建模型的实例

[英]Rails Admin - upload a CSV to create instances of a model

I have a straight forward model in my Ruby on Rails application that is also available in my rails_admin instance. 我的Ruby on Rails应用程序中有一个直接的模型,我的rails_admin实例中也提供了该模型。 Users of rails_admin will need to come in daily and upload a CSV or XLS file and based on some logic which I'll render on the backend, that will create instances of the model based on what data is in the CSV/XLS. rails_admin的用户需要每天进入并上传CSV或XLS文件,并根据我将在后端呈现的一些逻辑,这将根据CSV / XLS中的数据创建模型实例。 I don't need to persist the CSV or XLS on the filesystem, so that's not the problem. 我不需要在文件系统上保留CSV或XLS,所以这不是问题。 The issue is, I'm not entirely sure how to get an interface going in rails_admin where a user can upload a CSV/XLS, hit upload, and the backend should take care of the rest. 问题是,我不完全确定如何在rails_admin中获取接口,用户可以上传CSV / XLS,点击上传,后端应该处理其余部分。

Does rails_admin have support for this? rails_admin是否支持此功能? Can I create an interface through it where I can upload files for processing by one of my models? 我可以通过它创建一个界面,我可以上传文件以供我的某个模型处理吗?

It looks like you might have to create a custom action and view. 看起来您可能需要创建自定义操作和视图。 One way to do that would be to use this custom actions plugin . 一种方法是使用此自定义操作插件 There is also a tutorial about how to build a custom action here. 还有一个关于如何在此处构建自定义操作的教程。 I also used SmarterCSV, and it works brilliantly. 我也使用了SmarterCSV,它的工作非常出色。

To register a custom action with Rails Admin, you would do this in config/initializers/rails_admin.rb : 要使用Rails Admin注册自定义操作,您可以在config / initializers / rails_admin.rb中执行此操作:

module RailsAdmin
  module Config
    module Actions
      class YourClass < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

         ##code here, as explained more below

      end
    end
  end
end

In this class, you can inherit any of the base actions . 在此类中,您可以继承任何基本操作 So to register a custom partial, in that class you would do: 因此,要注册自定义部分,您将在该类中执行以下操作:

    # View partial name (called in default :controller block)
    register_instance_option :template_name do
      :your_class
    end

Your _your_class partials must be in app/views/rails_admin/main/, you can handle the form with multipart.. I'm not including the partial code, if you want me to take a swing at it let me know. 你的_your_class部分必须在app / views / rails_admin / main /中,你可以使用multipart处理表单..我不包括部分代码,如果你想让我挥杆它让我知道。

You will probably also want your action on the model scope: 您可能还希望对模型范围进行操作:

    register_instance_option :collection? do
      true
    end

And put your controller code in. It would probably be best to handle the processing here, for example: 并将控制器代码放入。最好在此处理处理,例如:

register_instance_option :controller do
      Proc.new do

        @order = Order.import(params[:file])
        f = SmarterCSV.process(file.tempfile)
              f.each do |r|

               #combine date and time fields 
               r[:date_time] = [r[:date],r[:time]].join(' ')

                Order.create("date" => r[:date_time])
        end
      end
    end

Next, your action should be registered with RailsAdmin::Config::Actions like this (This code was placed into config/initializers/rails_admin.rb): 接下来,您的操作应该使用RailsAdmin :: Config :: Actions注册(此代码放在config / initializers / rails_admin.rb中):

module RailsAdmin
  module Config
    module Actions
      class ApproveReview < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)
      end
    end
  end
end

Next, the custom action needs to be listed in the actions config in config/initializers/rails_admin.rb: 接下来,需要在config / initializers / rails_admin.rb中的actions配置中列出自定义操作:

RailsAdmin.config do |config|
  config.actions do
    dashboard
    index
    new

    your_class

    show
    edit
    delete
  end
end

There are more details in the tutorial, but I think that should be a pretty solid start! 本教程中有更多细节,但我认为这应该是一个非常可靠的开始!

You can create a custom action in RailsAdmin which will be in charge of getting the uploaded file and process it. 您可以在RailsAdmin中创建自定义操作,该操作将负责获取上载的文件并对其进行处理。

So in your file app/admin/your_model.rb you can add something like: 所以在你的文件app / admin / your_model.rb中你可以添加如下内容:

  member_action :upload_csv, :method => :post do
    # param[:file] will contain your uploaded file
    # So add your logic here to open/parse the file
    # Take a look at this link: http://railscasts.com/episodes/396-importing-csv-and-excel
  end

And in your view simply add a form with the multipart option 在您的视图中,只需添加带有multipart选项的表单

<%= form_tag import_products_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>

Look at this you can see that you can call what you want from the callback. 看看这个,你可以看到你可以从回调中调用你想要的东西。

Also you can create custom action to process your CSV. 您还可以创建自定义操作来处理CSV。

Or you can use existing plugin for CSV import. 或者您可以使用现有插件进行CSV导入。

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

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