Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

collection_select, undefined method `map' for nil:NilClass

I added in the @user = User.new line to make sure it isn't nil. Which object is nil?

undefined method `map' for nil:NilClass

Extracted source (around line #11):

8:  <%= f.hidden_field(:width)%>
9:  <%= f.hidden_field(:height)%>
10:     <% @user = User.new %>
11:     <%= collection_select(@user, :full_name, @user_array, :id, {:prompt => 'true'}, {:class=>'select'})%>
12:     <div class="submit-button">
13:         <%= submit_tag("Tag the person!")%>
14:     </div>
like image 370
Rose Perrone Avatar asked Dec 28 '22 00:12

Rose Perrone


2 Answers

@user_array is nil. Make sure it's set with an array of users to avoid this error message.

Alternatively, set @user_array to [] (an empty array) if you wish to show no options in the `select, e.g.:

collection_select @user, :full_name, @user_array || [], :id, 
  { prompt: 'true' }, { class: 'select' }
like image 109
Veraticus Avatar answered Feb 11 '23 20:02

Veraticus


You must to add @user_array to the actions: create, new, edit and update, the best way is using before_actions

before_action :set_user_array , only: [:edit, :update, :new, :create]

 private
    def set_user_array 
      @user_array = User.all 
    end
like image 29
Eduard Avendaño Avatar answered Feb 11 '23 20:02

Eduard Avendaño