title: Import data from user .csv file into table
body: <p>In a Ruby on Rails web app, I want to import a .csv file from a user and the data present in the .csv file will be inserted into the <code>questions</code> table, but I'm getting errors when I run the application.</p>
  
  <p>The app is working fine when I remove code written in <code>question.rb</code> and in the <code>html.erb</code> file.</p>
  
  <p>Controller code:
  <strong>questions_controller.rb</strong></p>
  
  <pre><code>class QuestionsController &lt; ApplicationController
    filter_access_to :all
  
    def import
      @question = Question.new
      @question.import(params[:file])
  
      CSV.foreach(file.path, headers: true) do |row|
        @question.save! row.to_hash
      end
  
      redirect_to root_url, notice: "Questions imported succefully."
    end
  
  end
  </code></pre>
  
  <p>Model code:
  <strong>question.rb</strong></p>
  
  <pre><code>def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      @question.save! row.to_hash
    end
  end
  </code></pre>
  
  <p>View code:
  <strong>question_type_listing.html.erb</strong></p>
  
  <pre><code>&lt;fieldset class="formContainer"&gt;
    &lt;legend&gt;&lt;%=t('question.select_qtype_cat')%&gt;&lt;/legend&gt;
  
    &lt;%= form_tag({:action =&gt; "import"}, {:id =&gt; "class_form"}, multipart: true) do %&gt;
  
      &lt;span style="width: 130px; float: left;"&gt;&lt;p&gt;&lt;label for="upload_file"&gt;Select File&lt;/label&gt; :&lt;/span&gt;
      &lt;%= file_field_tag :file %&gt;
      &lt;%= submit_tag "Import CSV" %&gt;
    &lt;% end %&gt;
  
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>Can someone suggest a way to fix this?</p>