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,如需转载请自行联系原作者

目录
打赏
0
0
0
0
234
分享
相关文章
ERROR [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer 1917: Failed to check/redeclare aut
ERROR [ntContainer#0-1] o.s.a.r.l.SimpleMessageListenerContainer 1917: Failed to check/redeclare aut
290 0
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 : ‘}‘
146 0
|
8月前
|
报错:error Parsing error: x-invalid-end-tag
报错:error Parsing error: x-invalid-end-tag
172 0
Uncaught runtime errors: × ERROR Avoided redundant navigation to current location: “/xxx“.
Uncaught runtime errors: × ERROR Avoided redundant navigation to current location: “/xxx“.
220 0
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
198 0
Error:fatal error C1010: unexpected end of file while looking for precompiled head
Error:fatal error C1010: unexpected end of file while looking for precompiled head
132 0
unknown type name err_status_t; did you mean srtp_err_status_t/err_status_ok/err_status_replay_fail
unknown type name err_status_t; did you mean srtp_err_status_t/err_status_ok/err_status_replay_fail
176 0
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
202 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等