Undefined method 'map' for nil:NilClass
My app seems to randomly be throwing a "undefined method `map' for nil:N开发者_StackOverflow社区ilClass" 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.
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@example.com", "network_id"=>"",
"password_confirmation"=>"[FILTERED]", "mobile"=>"", "password"=>"[FILTERED]",
"email"=>"email@example.com"}, "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.
Best guess...
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 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
You will get the error you are describing because @networkd_domestic
is not defined in the error condition in the update function.
Add @networkd_domestic = [...]
before the edit render and you should be good.
Is @networks_domentic
getting 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 nil
to the view.
EDIT:
If you look at the source for options_from_collection_for_select
you will see that it is calling map
on the collection you pass (@networks_domestic in this case).
精彩评论