我正在尝试编写让管理员上传充满法官的 CSV 的能力。当我尝试执行此操作时,我收到了一个 AssignmentError。这让我很困惑,因为我在所有相关变量上都使用了 attr_accessible。我所做的所有研究表明,如果您使用 attr_accessible,您应该能够批量分配变量。
我仍然是一个 Rails 新手,所以如果我在做一些非常愚蠢的事情,请原谅。
测试 CSV:
Name,Email,Competition_ID,Password
Brandon Schultz,b@schultz.com,1,12345678
Mr. Foo,foo@foo.com,1,12345678
Mr. Bar,bar@bar.com,1,12345678
错误信息:
ActiveModel::MassAssignmentSecurity::Error in JudgesController#import
Can't mass-assign protected attributes: Name, Email, Competition_ID, Password
应用程序跟踪:
app/models/judge.rb:17:in `block in import'
app/models/judge.rb:16:in `import'
app/controllers/judges_controller.rb:32:in `import'
这是相关的模型:
class Judge < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :competition_id
belongs_to :competition
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
Judge.create! row.to_hash
end
end
end
相关控制器:
def import
Judge.import(params[:file])
redirect_to admin_portal_path, notice: "Products imported."
end