2013-05-27 130 views
0

我有这样的代码:导轨 - 属性不保存

if request.post? and @user.save 
     @contact.user_id = @user.id 
     @contact.save 
     flash[:notice] = l(:e_user_saved) 
     redirect_to :action => 'list' 
end 

但是,当轨道保存联系人信息,user_ID的保持为空。

当我调试我看到它分配的值,但当它保存它保存为空。为什么???

+0

你的意思是'nil'在'contact'的'user',或两者兼而有之?你使用的是哪个数据库?它是否设置为在记录创建时自动生成'id'(主键)> – lurker

+3

保存返回的是什么?用'save!'替换它。也许验证失败 –

+0

是的,当然! –

回答

1

contact.save可能失败。你没有检查返回值,所以你不知道。

检查contact.errors散列以查看出了什么问题。

你或许应该有在ifcontact.save

if @contact.save 
    flash[:notice] = l(:e_user_saved) 
    redirect_to :action => 'list' 
else 
    # some error handling 
    # and redirect somewhere else 
end