collection_select,未定义的方法`map’为nil:NilClass

我在@user = User.new行添加了以确保它不是nil。 哪个对象是零?

 undefined method `map' for nil:NilClass Extracted source (around line #11): 8:  9:  10:  11:  'true'}, {:class=>'select'})%> 12: 
13: 14:

@user_arraynil 。 确保使用一组用户进行设置以避免出现此错误消息。

或者,如果您希望在`select中不显示任何选项,请将@user_array设置为[] (空数组),例如:

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

您必须将@user_array添加到操作:创建,新建,编辑和更新,最好的方法是使用before_actions

 before_action :set_user_array , only: [:edit, :update, :new, :create] private def set_user_array @user_array = User.all end