Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:36:03+00:00 2026-06-13T21:36:03+00:00

I have a form for import data like this: <%= form_tag({ controller: ‘courses’, action:

  • 0

I have a form for import data like this:

<%= form_tag({ controller: 'courses', action: :import }, multipart: true) do %> 
  <%= label_tag 'file', 'Import data' %>
  <%= file_field_tag 'file' %>
  <%= submit_tag "Import", name: nil, class: 'btn' %>
<% end %>

This is my import action:

def import
  require 'csv'

  csv_text = File.read(params[:file].tempfile.to_path.to_s)
  csv = CSV.parse(csv_text, headers: true )
  csv.each do |row|
    row = row.to_hash.with_indifferent_access
    Course.create(row.to_hash.symbolize_keys)
  end
  flash[:success] = "Successfully import data."
  redirect_to courses_url
end

But when i choose a file and press button Import in browser, i got error:

ActiveModel::MassAssignmentSecurity::Error in CoursesController#import
Can't mass-assign protected attributes: Name, Code

In my Course model, name and code already are attr_accessible:

class Course < ActiveRecord::Base
  attr_accessible :code, :name
end

What’s wrong with my code?

Updated
This is my csv file:

name, code
ERP System, HT555DV01
Data Mining, HT459DV01

New code to create data

csv.each do |row|
  Course.create!(name: row[0], code: row[1])
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:36:04+00:00Added an answer on June 13, 2026 at 9:36 pm

    try this

    csv.each do |row|
      row = row.to_hash.with_indifferent_access
      Course.create(row.to_hash.symbolize_keys)
    end
    

    replace to

      csv.each do |row|
        Course.create(row.to_hash)
      end
    

    Update

    csv_file = File.read(params[:file].tempfile.to_path.to_s)
    csv = CSV.parse(csv_file, :headers => true) 
    csv.each do |row|
       Course.create!(:name => row[0], :code => row[1])    
    end    
    

    Update 2

    csv_file = params[:file].read
    CSV.parse(csv_file) do |row|
       course = Course.create(row)
       course.save
    end
    

    source =>
    http://www.funonrails.com/2012/01/csv-file-importexport-in-rails-3.html
    http://erikonrails.snowedin.net/?p=212

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set the enctype to: multipart/form-data yet whenever I submit this form, the
I have a form in my forms.py that looks like this: from django import
I have a form like: #forms.py from django import forms class MyForm(forms.Form): title =
I have a form that allows a user to import a spreadsheet. This spreadsheet
I have a form like so: from django import forms from django.contrib.auth.models import User
I have form, where some fields are looks like rows, so I can add/delete
I have form in my page, I am using ajax.beginform(). Inside this form I
We're designing data import from an external source like MAS200 into our production SQL
I have some code to retrieve XML data: import cStringIO import pycurl from xml.etree
I have attached data at the bottom of this post. I have output from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.