collection_select,为 nil:NilClass 未定义的方法 `map' [英] collection_select, undefined method `map' for nil:NilClass

查看:54
本文介绍了collection_select,为 nil:NilClass 未定义的方法 `map'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

推荐答案

@user_arraynil.确保它设置了一组用户以避免出现此错误消息.

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

或者,如果您希望在 `select 中不显示任何选项,例如:

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' }

这篇关于collection_select,为 nil:NilClass 未定义的方法 `map'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆