【发布时间】:2011-03-29 10:24:12
【问题描述】:
我无法破解此错误。无法弄清楚为什么 @customer 被分配为零值。
“当你没有预料到它时,你有一个 nil 对象! 您可能期望 ActiveRecord::Base 的实例。 评估 nil.update_attributes 时发生错误"
这是一个sn-p的代码:
def cedit
@title = "Edit Customer Information"
@customer = Customer.find(params[:id])
if request.post? and params[:customer]
attribute = params[:attribute]
case attribute
when "fname"
try_to_update @customer, attribute
when "email"
try_to_update @customer, attribute
when "add"
try_to_update @customer, attribute
end
end
end
private
def try_to_update(customer, attribute)
if customer.update_attributes(params[:customer])
flash[:notice] = "Customer's details updated."
redirect_to :action => "record", :controller => "c2"
end
end
【问题讨论】:
-
你确定你正在做一个发布请求并且在参数中有一个
customer,你确定你不是在寻找id吗? -
你为什么要打破 REST 概念?我建议你读一本关于 Rails 的书。
-
是的,我在 params 中有一个客户。我正在为另一个动作和控制器使用类似的代码。那里工作正常..
-
你能告诉我我破坏了哪些 REST 概念吗?我是 RoR 的新手。
-
@mukul...你确定这是代码吗..
@customer中是否有错字在你打电话给try_to_update @customer, attribute的任何地方
标签: ruby-on-rails ruby null