Forum: Ruby on Rails importing .csv file from user and storing data of csv into database using ruby on rails.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B5f32b9c8ba4e391de49f1b92ad4d014?d=identicon&s=25 unknown (Guest)
on 2015-08-24 10:28
(Received via mailing list)
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. 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])
    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”])

if @question.count == 1
@question.first.update_attributes(question_hash)
else
Question.save!(question_hash)
end # end if !@question.nil?
end # end CSV.foreach
end # end self.import(file)

question_type_listing.html.erb


<fieldset class="formContainer">
<legend><%=t('question.select_qtype_cat')%></legend>


    <%= form_tag({:action => "import"}, multipart: true) do %>

    <span style="width: 130px; float: left;"><p><label
for="upload_file">
Select File</label> :</span>
    <%= file_field_tag :file %>
    <%= submit_tag "Import CSV" %>
    <% end %>

</fieldset>

When i placed this code in respective modules . And restarted server
apart
from this all working fine . Please help me to resolve this issue .

Thanks and regards
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2015-08-24 14:10
(Received via mailing list)
On Monday, August 24, 2015 at 9:28:12 AM UTC+1,
iampraveennarahari@gmail.com wrote:
>
Check your log file (development.log) - it should contain extra details
about the problem (such as a stack trace)


> I am using Ruby  1.8.7
> Rails 3.0.3
>
> Just fyi, both of these 2 release series are unmaintained. Rails 3.0.3 has
several remote code execution flaws

>
> if @question.count == 1
> @question.first.update_attributes(question_hash)
> else
> Question.save!(question_hash)
> end # end if !@question.nil?
>

This line looks fishy - there is no class method called save!. Perhaps
you
meant create! ?

Fred
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2015-08-27 22:03
(Received via mailing list)
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.


> 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])



>
>
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
75caea9632dfb35b8259dd6e4e4772c6?d=identicon&s=25 Elizabeth McGurty (Guest)
on 2015-08-28 16:16
(Received via mailing list)
def import
   @question = Question.new    ## You have just created a new instance
of
model class Question
   @question.import(params[:file])
      redirect_to root_url, notice: "Questions imported succefully."
  end

class Question
validates :id, uniqueness: true   ## see below
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|    ## okay so you will
iterate by row
question_hash = row.to_hash
                        ## This is okay, but you should put a watch on
row,
and understand its structure.  In doing so, you may not need to convert
row
to row.to_hash.  Otherwise, you should put a watch on row.to_hash to
verify
that the
                        ## structure of the 'row' is consistent with
your
database structure  and other matters that may be in your Question model
                        ##@question = Question.where(id:
question_hash[“id”])
                        ## okay this seems reasonable  but at the model
level rather than creating a new Question object, you should rather be
doing validation on id, see above.
                        ##  If I were you I would do validation on the
record, not just the id
    ## if @question.count == 1
    ##@question.first.update_attributes(question_hash)   ## self not new
instance of Question
@return_value_on_update_attributes =
self.update_attributes(question_hash)     ## Read:
http://apidock.com/rails/ActiveRecord/Base/update_attributes
unless @return_value_on_update_attributes  ## unless
@return_value_on_update_attributes is false, eg, if not true
    puts "There was an error in record " +     question_hash[“id”]
    puts self.errors.to_yaml      ## This way you can continue, and then
later you need to check for errors.  Of course there are other ways to
manage and learn errors.  But for now...
end  # end unless
end  # end foreach

end # end self.import(file)
75caea9632dfb35b8259dd6e4e4772c6?d=identicon&s=25 Elizabeth McGurty (Guest)
on 2015-08-28 16:40
(Received via mailing list)
Correction: obviously class Question  < ActiveRecord::Base
This topic is locked and can not be replied to.