Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined method '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.

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.

like image 936
Shpigford Avatar asked Nov 12 '09 17:11

Shpigford


2 Answers

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.

like image 144
Tony Fontenot Avatar answered Nov 01 '22 16:11

Tony Fontenot


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).

like image 6
Jerry Fernholz Avatar answered Nov 01 '22 15:11

Jerry Fernholz