Save 36% for Black Friday! Learn more

All threads / Import into administrate?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Import into administrate?

Martin Curiel asked in Rails

Hi - I used Jumpstart and really like the administrate gem relative to ActiveAdmin. One issue that I am having a hard time is the import add-on doesn't seem to be available like it is for ActiveAdmin (I found export add-on here: https://github.com/thoughtbot/administrate/wiki/List-of-Plugins) Do you have any thoughts on this? Should I just build out an import functionality from scracth?

Imports are usually pretty unique to your app, so I imagine they intend it to be that way since your format may not exactly match the database table.

There's several episodes here on GoRails talking about importing from CSV. I'd recommend checking those out and building that out for importing with Administrate.

Hi, I think Jumpstart looks really promising and I thinking about using it for my next project.

But I also had some concerns about importing data to my app. I'm used to be doing import via the active-admin-import gem. In the application I'm working on I need to import lots of CSV-files and some of the columns include remote-links to images that I want to import and store on my app/cloud storage.

Is there any Go Rails Episode covering uploading images from CSV files? I can't find much information on that matter.

@Tommy, importing images from CSVs would be as simple as assigning the image to the record using the URL from the CSV.

For example, Shrine's remote url plugin would be all you'd need. https://github.com/shrinerb/shrine/blob/master/doc/plugins/remote_url.md

If your uploader doesn't support url uploads, you'd need to download the image temporarily using like OpenURI in Ruby and then assign the file object to the record before saving.

Thanks for your reply @Chris!

Been looking at your tutorials for Shrine and CSV imports. And trying to get this to work, using and rake import task. But I'm getting an error when I run the rake task.

rake aborted!
ArgumentError: bad argument (expected URI object or URI string)

Here's my code. Any help to this, would be very much appreciated.

** /lib/tasks/import.rake **

require 'csv'
namespace :import do
desc "Import stadiums from csv"
task stadiums: :environment do
filename = File.join Rails.root, "stadiums.csv"
counter = 0

    CSV.foreach(filename, headers: true) do |row|
    stadium = Stadium.create(name: row["name"],city: row["city"],image: Shrine.remote_url(row["coverphoto"]))

    counter += 1 if stadium.persisted?
    end
    puts "Imported #{counter} stadiums"
    end
    end


** app/uploaders/image_uploaders.rb
 **
class ImageUploader < Shrine
Attacher.validate do
validate_size 1..5*1024*1024
validate_mime_type %w[image/jpeg image/png image/webp image/tiff]
validate_extension %w[jpg jpeg png webp tiff tif]
end
end

/shrine.rb

require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/memory"
require "shrine/storage/s3"

if Rails.env.test?
Shrine.storages = {
cache: Shrine::Storage::Memory.new, # temporary
store: Shrine::Storage::Memory.new, # permanent
}
else
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"), # permanent
}
end

And in my stadiums_controller I’ve added :image as param.
def stadium_params
params.require(:stadium).permit(:name, :city, :image)
end

The error sounds like you maybe didn't pass in a URL properly in somewhere. Print out the row["coverphoto"] to make sure it's a valid URL.

Also you should change the line to this:

    stadium = Stadium.create(name: row["name"],city: row["city"], image_remote_url: row["coverphoto"])

That will let Shrine handle it seamlessly for you.

Ah, that solved my issue. Now it works! :)

Thank you so much for your help and these fantastic resources!

You're welcome! Glad that worked out pretty easily!

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 39,609+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.