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?
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!
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 %>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.