little tip (The error occurred while evaluating nil.update_attributes)

简介:
这种错误一般发生在,还没有创建的情况下就进行edit+update操作。
譬如在blog应用里,新建了一个blog对象,它关联一个logo对象,当你创建了blog对象时,logo对象还未创建,此时如果修改blog,并且还捎带的上传logo的话就会发生nil.update_attributes错误。
 
为了解决这个问题,可以在logos_controller的update方法里这样写:
 
    @logo=(@blog.avatar ||= @blog.create_avatar)
 
或者这样写:
 
@logo= Logo.find_or_create_by_blog_id(@blog.id)
 




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/124250,如需转载请自行联系原作者

目录
相关文章
|
2月前
|
Perl
报错:error Parsing error: x-invalid-end-tag
报错:error Parsing error: x-invalid-end-tag
|
9月前
error C2449: found ‘{‘ at file scope (missing function header?)和error C2059: syntax error : ‘}‘
error C2449: found ‘{‘ at file scope (missing function header?)和error C2059: syntax error : ‘}‘
72 0
|
10月前
|
关系型数据库 MySQL C++
Error:error C2601: ‘b‘ : local function definitions are illegal error C2063: ‘b‘ : not a function
Error:error C2601: ‘b‘ : local function definitions are illegal error C2063: ‘b‘ : not a function
124 0
|
11月前
|
机器学习/深度学习 Windows
raise RuntimeError(‘Error(s) in loading state_dict for {}:\n\t{}‘.format( RuntimeError: Error(s)..报错
即load_state_dict(fsd,strict=False) 属性strict;当strict=True,要求预训练练权重层数的键值与新构建的模型中的权重层数名称完全吻合;
1174 0
解决报错之 - error Identifier ‘attr_id‘ is not in camel case camelcase
解决报错之 - error Identifier ‘attr_id‘ is not in camel case camelcase
366 0
解决报错之 - error Identifier ‘attr_id‘ is not in camel case camelcase
|
计算机视觉
opencv出错:error: (-213:The function/feature is not implemented) Unknown/unsupported array type
opencv出错:error: (-213:The function/feature is not implemented) Unknown/unsupported array type
403 0
成功解决ParserError: Error tokenizing data. C error: Expected 2 fields in line 53, saw 3
成功解决ParserError: Error tokenizing data. C error: Expected 2 fields in line 53, saw 3
Whitelabel Error Page There was an unexpected error (type=Not Found, status=404). No message availab
Whitelabel Error Page There was an unexpected error (type=Not Found, status=404). No message availab
148 0
|
SQL 关系型数据库 数据库
[Err] ERROR: invalid input syntax for integer: "1.0"
[Err] ERROR: invalid input syntax for integer: "1.0"
957 0
[Err] ERROR: invalid input syntax for integer: "1.0"