未定义的方法 'map' 为 nil:NilClass [英] Undefined method 'map' for nil:NilClass
问题描述
当用户尝试更新他们的个人资料时,我的应用似乎随机抛出未定义的方法`map' for nil:NilClass"错误.
My app seems to randomly be throwing a "undefined method `map' for nil:NilClass" error when users are trying to update their profile.
但奇怪的是,它说错误发生在更新时,但错误行实际上在视图中.
But what's weird is it's saying the error happens on update, but the error line is actually in a view.
完全错误:
users#update (ActionView::TemplateError) "undefined method `map' for nil:NilClass"
On line #52 of app/views/users/edit.html.erb
Line 52: <%= options_from_collection_for_select(@networks_domestic, 'id', 'name', @user.network_id) %>
这里是最近错误的参数:
And here are the params from a recent error:
{"user"=>{"email_notify"=>"email@example.com", "network_id"=>"",
"password_confirmation"=>"[FILTERED]", "mobile"=>"", "password"=>"[FILTERED]",
"email"=>"email@example.com"}, "action"=>"update", "_method"=>"put", "id"=>"5089",
"controller"=>"users"}
老实说,我什至不确定从哪里开始寻找.我有一个用户说他可以从 IE 更新相同的信息,但不能从 Firefox 更新.当我使用他们相同的信息时,我可以毫无问题地进行更新.所以,我很难过.
Honestly not sure where to even start looking. I've had a user say he can update the same info from IE but not from Firefox. And when I use their same info I'm able to update without issue. So, I'm stumped.
推荐答案
最好的猜测...
您的编辑函数正确定义了@networks_domestic
,所以一切都很好,直到您在更新函数中遇到错误并调用render :action =>编辑"
.
Your edit function properly defines @networks_domestic
so everything is great until you encounter an error in the update function and call render :action => "edit"
.
Render 不调用编辑函数,而只是渲染编辑视图.因此,在更新失败的情况下,您必须在从更新返回之前定义 @networks_domestic
.
Render does not call the edit function but rather just renders the edit view. So, in the case of a failed update you will have to define @networks_domestic
before returning from update.
比如说,你有以下内容:
So say, for example, you have the following:
def edit
@user = User.find(params[:id])
@networkd_domestic = [...]
end
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = "User was successfully updated."
format.html { redirect_to(admin_users_url) }
else
format.html { render :action => "edit" }
end
end
end
您将收到您所描述的错误,因为 @networkd_domestic
未在更新函数的错误条件中定义.
You will get the error you are describing because @networkd_domestic
is not defined in the error condition in the update function.
在编辑渲染之前添加@networkd_domestic = [...]
,你应该会很好.
Add @networkd_domestic = [...]
before the edit render and you should be good.
这篇关于未定义的方法 'map' 为 nil:NilClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!