Ruby on Rails
Sunday, August 23, 2015
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
questions.rb
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
-- 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
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/b7e60f3a-4d11-4bec-b882-9ca32271df64%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment