Collection_select, undefined `map 'method for nil: NilClass

I added to the string @user = User.newto make sure it is not null. Which object is zero?

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>
+3
source share
2 answers

@user_array nil. Make sure that it is configured by many users to avoid this error message.

Alternatively, set @user_arrayto [](empty array) if you do not want to display any parameters in `select, eg .:

collection_select @user, :full_name, @user_array || [], :id, 
  { prompt: 'true' }, { class: 'select' }
+7
source

You should add @user_array to the actions: create, update, edit and update, the best way is to use before_actions

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

 private
    def set_user_array 
      @user_array = User.all 
    end
0
source

All Articles