Rails Admin - load CSV to create model instances

I have a direct model in my Ruby on Rails application, which is also available in my rails_admin instance. Rails_admin users will have to log in and download a CSV or XLS file daily and based on some logic that I will map to the backend, which will create model instances based on the data stored in CSV / XLS. I do not need to save CSV or XLS in the file system, so that is not a problem. The problem is that I'm not quite sure how to get the interface going to rails_admin, where the user can download CSV / XLS, delete the download, and the backend has to take care of everything else.

Does rails_admin support this? Can I create an interface through it where I can upload files to process one of my models?

+7
ruby ruby-on-rails rails-admin
source share
3 answers

It looks like you might have to create a custom action and view. One way to do this is to use this custom action plugin . There is also a tutorial on how to create a custom action here. I also used SmarterCSV and it works great.

To register a custom action with the Rails Admin, you must 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 basic actions . So, to register a user partial, in this class you would do:

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

Your partial parts of your _your_class should be in app / views / rails_admin / main /, you can process the form using multipart. I do not include partial code, if you want me to make a swing in it, let me know.

You may also need your actions in the area of ​​the model:

  register_instance_option :collection? do true end 

And enter the controller code. It is probably 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 

Then your action should be logged in RailsAdmin :: Config :: Actions like this (this code was placed in 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 

Then user actions should be specified in the action configurations in config / initializers / rails_admin.rb:

 RailsAdmin.config do |config| config.actions do dashboard index new your_class show edit delete end end 

The tutorial has more detailed information, but I think it should be a pretty solid start!

+4
source share

You can create a custom action in RailsAdmin that will be responsible for receiving the downloaded file and processing it.

So, in your app / admin / your_model.rb file, 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 opinion, just add a form with the multipart parameter

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

Look at this one , you can see that you can call what you want from the callback.

You can also create a custom action to handle CSV.

Or you can use an existing plugin to import CSV.

-one
source share

All Articles