0

I have been trying to import data to my ruby on rails aplication 4.2 hosted in heroku with a database in pg. I tried this tutorial and it didnt worked:

http://railscasts.com/episodes/396-importing-csv-and-excel?view=asciicast Say no method import in dreams controller.

Any body knows a good way to import data to my app? Thanks! Felipe

  • Silly question but, did you remember to push the files to the remote? – Joao Cunha Nov 14 '14 at 21:28
  • I dont have connection between my db in local (Mysqlite) and the db in remote (Pg). Im migrating the data from another version of the website to the new one in ruby on rails. So im trying to prove with csv, to begin migrating the information. I don't know if this is your question? i'm right? Thanks for your collaboration! – user2284348 Nov 14 '14 at 21:36
  • This is the screenshot of the error: https://www.evernote.com/shard/s447/sh/bc54becd-2c39-4ec7-b139-f9defc7651f0/9caea62319ac2ecad30ac0738931a649 – user2284348 Nov 14 '14 at 22:04
  • Is the local instance the same application as you're running on Heroku? Are you only changing databases or are you also changing applications? – Dan Rice Nov 15 '14 at 01:57

2 Answers2

0

If it says no method import in dreams_controller it would appear that you did not properly define the import method. In other words, you either skipped or messed up this portion of the tutorial:

/app/controllers/products_controller.rb

def import
  Product.import(params[:file])
  redirect_to root_url, notice: "Products imported."
end

If you could post the code it would be much easier to help.

mattforni
  • 855
  • 5
  • 11
  • Thanks for the help! This is my dreams_controller.rb: def import Dream.import(params[:file]) redirect_to root, notice: "Dreams imported." end end – user2284348 Nov 14 '14 at 22:21
  • If you look at the tutorial, it takes the path of the file to be imported as the parameter in params. IE. `Product.import(params[:file])` Then the `import` method on the `Dream` model parses that file. You're passing `id` as the param with a value of `import` which just isn't going to work. – mattforni Nov 14 '14 at 22:43
  • Hi Matt, thanks man! look, i'm trying this:private def set_dream @dream = Dream.find(params[Rails.root + "app/csv/estructura.csv"]) end this is the result: https://www.evernote.com/shard/s447/sh/0b40a56f-e21a-4404-8046-fdd85f887460/1ab9b0e0cb7d7885afa07708d08f4d70 – user2284348 Nov 15 '14 at 00:19
  • So here's the deal ... the `find` method ***always*** expects a database ID as a parameter and ***always*** raises an exception if it cant find a model with that ID in the database. Obviously you don't have a `Dream` object in the database with a big long path name as it's ID. Change `Dream.find(...)` to `Dream.import(File.join(Rails.root, 'app/csv/estructura.csv'))` and you should be good to go. – mattforni Nov 16 '14 at 15:35
  • Hi Mat! Thanks for your answer man! i dont have Dream.find in my dream controller. Any way i did what you said and the result is: https://www.evernote.com/shard/s447/sh/f0d0c94f-00e7-4beb-9273-263446b67d46/a7832a39089fa167ca192df1398c103c – user2284348 Nov 16 '14 at 21:10
0

The missing method is inside dream.rb model . You need this:

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    Dream.create! row.to_hash
  end
end

That is because when you call Dream.import(params[:file]) you are actually calling the class method of the Dream model.

Joao Cunha
  • 772
  • 4
  • 15
  • This is my dream.rb: def self.iported(file) CSV.foreach(file.path, headers: true) do ⎮row⎮ Dream.create! row.to_hash end end – user2284348 Nov 14 '14 at 22:22
  • self.iported should be self.import . Was that a typo in stackoverflow or in your code? – Joao Cunha Nov 14 '14 at 22:24
  • Could you explain me a little bit more? :) Thanks! i made it as in the tutorial, what i'm doing wrong? – user2284348 Nov 14 '14 at 22:24
  • This is what i get: https://www.evernote.com/shard/s447/sh/5a99df73-f4a6-405e-8b4e-6785a6c3025f/1d6e1e2a7285560241137e65e39c31ed In my code – user2284348 Nov 14 '14 at 22:29
  • Looking to the error image you've posted, we can say for sure that your action in the controller is being called. GREAT! However, the error complains that the function IMPORT does not exists for the class DREAM that you are trying to use when you do Dream.import inside the controller. Altough, the definition of Dream.import is inside of dream.rb so you must have copied and pasted something wrong in this model – Joao Cunha Nov 14 '14 at 22:30
  • This is what i have in my dreams_controller: def import Dream.import(params[:file]) redirect_to root, notice: "Dreams imported." end end – user2284348 Nov 14 '14 at 22:34
  • You mean that the problem is solved and mabe i am not importing the right csv, with the right structure of database? – user2284348 Nov 14 '14 at 22:43
  • You need to pass the path to the file to be imported. – mattforni Nov 14 '14 at 22:43
  • Sorry, I was not sure of what I've said in this last comment, so I took it off. Tell me something, your root_path is the Dream show action? – Joao Cunha Nov 14 '14 at 22:45
  • I found this example in the example code of the tutorial: def self.import(file) spreadsheet = open_spreadsheet(file) header = spreadsheet.row(1) (2..spreadsheet.last_row).each do |i| row = Hash[[header, spreadsheet.row(i)].transpose] product = find_by_id(row["id"]) || new product.attributes = row.to_hash.slice(*accessible_attributes) product.save! end end could you explain me where i have to add the path of the file please? i must to replace param with this path? – user2284348 Nov 14 '14 at 23:00
  • I'trying with this path: /Users/felipevelasquez/Desktop/SOS/LAPAPAYA/DB/estructura.csv but its not working: https://www.evernote.com/shard/s447/sh/69aec505-4145-44db-95fc-08ea540f750c/e7405d14cdc1d2860ac4a1e9b34aa6db sorry! i'm newe, and i have been trying for this for a months, thanks for your collaboration! – user2284348 Nov 14 '14 at 23:21
  • Here i continue working! https://www.evernote.com/shard/s447/sh/95a27450-b5a4-45ab-944b-aaa7206d9329/c744e68a7e4e23e832a274ac85bb5071 – user2284348 Nov 15 '14 at 00:18