Ruby-on-rails 未定义的方法 'map' 为 nil:NilClass
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1723960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined method 'map' for nil:NilClass
提问by Shpigford
My app seems to randomly be throwing a "undefined method `map' for nil:NilClass" error when users are trying to update their profile.
当用户尝试更新他们的个人资料时,我的应用程序似乎随机抛出“未定义的方法`map' for nil:NilClass”错误。
But what's weird is it's saying the error happens on update, but the error line is actually in a view.
但奇怪的是,它说错误发生在更新时,但错误行实际上在视图中。
Full error:
完整错误:
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 protected]", "network_id"=>"",
"password_confirmation"=>"[FILTERED]", "mobile"=>"", "password"=>"[FILTERED]",
"email"=>"[email protected]"}, "action"=>"update", "_method"=>"put", "id"=>"5089",
"controller"=>"users"}
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.
老实说,不知道从哪里开始寻找。我有一个用户说他可以从 IE 更新相同的信息,但不能从 Firefox 更新。当我使用他们相同的信息时,我可以毫无问题地进行更新。所以,我很难过。
回答by Tony Fontenot
Best guess...
最佳的揣测...
Your edit function properly defines @networks_domesticso everything is great until you encounter an error in the update function and call render :action => "edit".
您的编辑功能正确定义,@networks_domestic所以一切都很好,直到您在更新功能中遇到错误并调用render :action => "edit".
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_domesticbefore returning from update.
Render 不调用编辑功能,而只是呈现编辑视图。因此,在更新失败的情况下,您必须@networks_domestic在从更新返回之前定义。
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
You will get the error you are describing because @networkd_domesticis not defined in the error condition in the update function.
您将收到您所描述的错误,因为@networkd_domestic未在更新函数的错误条件中定义。
Add @networkd_domestic = [...]before the edit render and you should be good.
@networkd_domestic = [...]在编辑渲染之前添加,你应该很好。
回答by Jerry Fernholz
Is @networks_domenticgetting set properly in the controller? Add <%= @networks_domestic.inspect %>right before line 52 and see what you get. Check for @networkd_domestic.nil?in the controller and make sure you don't send nilto the view.
是否@networks_domentic在控制器中正确设置?<%= @networks_domestic.inspect %>在第 52 行之前添加,看看你得到了什么。检查@networkd_domestic.nil?控制器并确保您没有发送nil到视图。
EDIT:
编辑:
If you look at the source for options_from_collection_for_selectyou will see that it is calling mapon the collection you pass (@networks_domestic in this case).
如果您查看源代码,options_from_collection_for_select您会发现它正在调用map您传递的集合(在本例中为 @networks_domestic)。

