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

但是当 rails 保存联系人时,user_id 仍然为空。

当我调试时,我看到它分配了值,但是当它保存时它保存了 null。为什么???

4

1 回答 1

1

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

检查contact.errors哈希以查看问题所在。

你可能应该有contact.save一个if块:

if @contact.save
  flash[:notice] = l(:e_user_saved)
  redirect_to :action => 'list'
else
  # some error handling
  # and redirect somewhere else
end
于 2013-05-27T15:34:20.597 回答