我是 Ruby on Rails 的新手。
我正在使用 Rails 4.0.3、Ruby 1.9.3。
我试图导入CSV file from the sample "396-importing-csv-and-excel-master"
但它抛出错误。
错误:
attr_accessible 不再使用
并建议使用 Strong 参数。任何人都可以帮助我使用强参数导入 CSV 吗?
我是 Ruby on Rails 的新手。
我正在使用 Rails 4.0.3、Ruby 1.9.3。
我试图导入CSV file from the sample "396-importing-csv-and-excel-master"
但它抛出错误。
错误:
attr_accessible 不再使用
并建议使用 Strong 参数。任何人都可以帮助我使用强参数导入 CSV 吗?
假设您正在导入任务。对强参数使用这种方式
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
task = find_by_id(row["id"]) || new
parameters = ActionController::Parameters.new(row.to_hash)
task.update(parameters.permit(:id,:name))
task.save!
end
end
attr_accessible 在 rails 4 中不再使用。在 rails4 中就像params.require(:person).permit(:name, :age)
我认为这可以帮助你