Rails Admin - upload a CSV to create instances of a model
Asked Answered
C

3

9

I have a straight forward model in my Ruby on Rails application that is also available in my rails_admin instance. 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. I don't need to persist the CSV or XLS on the filesystem, so that's not the problem. 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.

Does rails_admin have support for this? Can I create an interface through it where I can upload files for processing by one of my models?

Convalescence answered 29/10, 2013 at 2:25 Comment(6)
Which gem you are using for upload? and what you exactly want. do you want to upload the file in back end or process the csv/xls data in the back ground.Decrescendo
I just want to upload the file, and process it in the background. Something as simple as POSTing the file and then I can set off an async job. I'm not necessarily managing a model or instances of a class with this file directly from rails admin. I just need a way for people to upload it through there.Convalescence
lots of way you can do it. best way is just upload the file using paperclip or carrier wave gem. then ruby has its csv library to process it. and use whenever gem or just simple make a thread to process it in the back ground.Decrescendo
I get that. What I don't get is how to integrate that UI into rails_admin.Convalescence
I am not sure if rails admin have this facility. but if it does not have support for this create your custom method and view for this.Decrescendo
I think the plugin listed by freemanoid down below is what I need, just verifying it works - "rails_admin_import"Convalescence
D
5

You can create a custom action in RailsAdmin which will be in charge of getting the uploaded file and process it.

So in your file app/admin/your_model.rb you can add something like:

  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

<%= form_tag import_products_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>
Dogear answered 6/11, 2013 at 9:52 Comment(1)
You are talking about ActiveAdmin, but the question is for RailsAdmin.Plage
G
5

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.

To register a custom action with Rails Admin, you would do this in 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.

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):

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:

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!

Guss answered 7/11, 2013 at 1:46 Comment(0)
L
-1

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.

Or you can use existing plugin for CSV import.

Lubricator answered 31/10, 2013 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.