[英]Cant mass assign protected attributes in Rails
控制器:
class HorsesController < ApplicationController
require 'csv'
def index
@horses = Horse.all
end
def import
Horse.import(params[:file])
redirect_to root_path
end
end
模型:
class Horse < ActiveRecord::Base
attr_accessible :name, :place
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Horse.create! row.to_hash
end
end
end
我有一個包含兩列數據標題名稱和位置的CSV文件。 當我嘗試導入文件時,出現錯誤消息: Can't mass-assign protected attributes: place
我似乎接受:name很好,但由於某些原因將無法與:place一起使用 ?
任何幫助表示贊賞
您的csv文件格式類似於name, place
而不是name,place
。 開始place
讀取為<space>place
而不是使rails拋出該錯誤的place
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.