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

时间:2012-05-24 16:44:46

标签: ruby-on-rails

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

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>

2 个答案:

答案 0 :(得分:8)

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

或者,如果您希望在`select中没有选项,例如:

,请将@user_array设置为[](空数组)
collection_select @user, :full_name, @user_array || [], :id, 
  { prompt: 'true' }, { class: 'select' }

答案 1 :(得分:0)

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

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

 private
    def set_user_array 
      @user_array = User.all 
    end