我们的rails 3.1.4 app中的sys_log模型中定义了一个质量分配:
attr_accessible :log_date, :user_name, :user_id, :user_ip, :action_logged, :as => :new_log
在application_controller中定义了一个方法来保存日志:
def sys_logger(action_logged)
log = SysLog.new(:log_date => Time.now, :user_id => session[:user_id], :user_name => session[:user_name], :user_ip => session[:user_ip],
:action_logged => action_logged, :as => :new_log)
log.save
end
然而,质量分配不起作用。这是警告信息:
WARNING: Can't mass-assign protected attributes: log_date, user_id, user_name,
user_ip, action_logged, as
:new_log未按定义运行。上面的代码有什么问题?非常感谢。
答案 0 :(得分:1)
:as => :new_log
现在是属性哈希的一部分,而不是您传入的单独选项。
添加一些花括号应该有帮助:
def sys_logger(action_logged)
log = SysLog.new({:log_date => Time.now, :user_id => session[:user_id],
:user_name => session[:user_name], :user_ip => session[:user_ip],
:action_logged => action_logged }, :as => :new_log)
log.save
end
暂时分配:
def sys_logger(action_logged)
attrs = { :log_date => Time.now, :user_id => session[:user_id],
:user_name => session[:user_name], :user_ip => session[:user_ip],
:action_logged => action_logged }
log = SysLog.new(attrs, :as => :new_log)
log.save
end