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.
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_domesticso everything is great until you encounter an error in the update function and callrender :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.So say, for example, you have the following:
You will get the error you are describing because
@networkd_domesticis not defined in the error condition in the update function.Add
@networkd_domestic = [...]before the edit render and you should be good.