从csv文件导入数据时,无法批量分配受保护的属性 [英] Can't mass-assign protected attributes when import data from csv file
问题描述
我有一个用于导入如下数据的表单:
<%= form_tag({controller:'courses' action::import},multipart:true)do%>
<%= label_tag'file','导入数据'%>
<%= file_field_tag'file'%>
<%= submit_tagImport,name:nil,class:'btn'%>
<%end%>
这是我的导入操作:
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] =成功导入数据。
redirect_to courses_url
end
但是当我选择一个文件, code>在浏览器中导入时出现错误:
ActiveModel :: MassAssignmentSecurity ::错误在CoursesController#import
无法大量分配受保护的属性:名称,代码
在我的课程
模型中, name
和 code
已经是attr_accessible :
class Course< ActiveRecord :: Base
attr_accessible:code,:name
end
更新
这是我的csv档案:
名称,代码
ERP系统,HT555DV01
数据挖掘,HT459DV01
创建数据的新代码
csv。 each do | row |
Course.create!(name:row [0],code:row [1])$ b $ b end
尝试此
csv.each do | row |
row = row.to_hash.with_indifferent_access
Course.create(row.to_hash.symbolize_keys)
end
替换为
csv.each do | row |
Course.create(row.to_hash)
end
更新
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])$ b $ b end
更新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
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
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
这篇关于从csv文件导入数据时,无法批量分配受保护的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!