[Rails] Re: importing .csv file from user and storing data of csv into database using ruby on rails.
Ruby on Rails
Thursday, August 27, 2015
On Monday, 24 August 2015 04:28:12 UTC-4, iampravee...@gmail.com wrote:
Hi friends , I am modifying virtualX open source application. I wanted to introduce new functionality that is uploading questions from .csv file and storing those questions to the database. I am very new to ruby this task becomes very tough for me . So i referred so many websites and blogs and i ready some code for this feature . but this is code is not working . I am getting HTTP 500 error . As i checked code in Aptanan Studio , it is showing some errors.
In future, please also post the errors; since this isn't all the code in your application we can't run it to see what happens, and are relying on you to tell us.
I am posting my code over here . Please help me friends . I am really not getting what is happening .
I am using Ruby 1.8.7
Rails 3.0.3
questions_controller.rb
def import
@question = Question.new
@question.import(params[:file])
You've defined the import method below as a class method (`def self.import`) but are attempting to call an instance method here on `@question`.
The two lines above should likely be:
Question.import(params[:file])
redirect_to root_url, notice: "Questions imported succefully."
end
questions.rb
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
question_hash = row.to_hash
@question = Question.where(id: question_hash["id"])
Double-check your source file: the line you've pasted here has slant-quotes (" and ") which are not the same thing to Ruby as a straight double-quote ("). It's also possible that these were adding while composing the email.
if @question.count == 1
@question.first.update_attributes (question_hash)
else
Question.save!(question_hash)
What is the intent of this line? Are there situations where you can have multiple `Question` records with the same ID? There is not a class method called `save!` built into ActiveRecord.
--Matt Jones
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/8f92e388-a655-4489-aaa1-badd7370a74c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment