Ruby on rails Rails试图保存集合为空的表单时,会给出未定义的方法“map';零级:零级

Ruby on rails Rails试图保存集合为空的表单时,会给出未定义的方法“map';零级:零级,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有一个表单,其中包含一个从集合\u select生成的下拉列表。集合选择以空白开始,然后当用户从日期字段中选择日期时,集合选择将使用所选日期的值进行更新 如果提交表单时没有在下拉列表中选择值,我会尝试显示一条漂亮的错误消息。目前,我遇到了以下错误:undefined methodmap'for nil:NilClass` 如果用户没有在下拉列表中选择某个值,我如何使其显示一条漂亮的错误消息 看法 给出的错误是指在试图保存表单时@available_时隙为空我会尝试类似的方法 def新建 @可用

我有一个表单,其中包含一个从集合\u select生成的下拉列表。集合选择以空白开始,然后当用户从日期字段中选择日期时,集合选择将使用所选日期的值进行更新

如果提交表单时没有在下拉列表中选择值,我会尝试显示一条漂亮的错误消息。目前,我遇到了以下错误:
undefined method
map'for nil:NilClass`

如果用户没有在下拉列表中选择某个值,我如何使其显示一条漂亮的错误消息

看法


给出的错误是指在试图保存表单时@available_时隙为空

我会尝试类似的方法

def新建
@可用的\u时隙=。。。
如果@available\u timeslots.count>0
flash.now[:error]=“nil errrraarrr”
结束
...
结束
鉴于


0 %>

@可用的\u时隙
为零。确保设置了可用时隙数组以避免此错误消息。

诀窍是在else子句中添加
@available\u timeslots=[]

def create
    @arrangement = Arrangement.new(params[:arrangement])

    respond_to do |format|
      if @arrangement.save
        # update the timslot record with the arrangement id
        if @timeslot = Timeslot.update(@arrangement.timeslot_id, :arrangement_id => @arrangement.id)
            format.html { redirect_to @arrangement, notice: 'Arrangement was successfully created.' }
            format.json { render json: @arrangement, status: :created, location: @arrangement }
        else
            format.html { render action: "new" }
            format.json { render json: @arrangement.errors, status: :unprocessable_entity }
        end
      else

        format.html { render action: "new" }
        format.json { render json: @arrangement.errors, status: :unprocessable_entity }
      end
    end
  end

这就是我问这个问题的原因。我知道它是零,但是在提交表单时,如果没有填写它,我怎么能显示一个好的错误呢。你可以用。blank检查它?还是用。零?在控制器端。并且可以轻松地闪现错误消息。这在我的情况下不起作用,因为集合选择会根据他们选择的日期进行ajaxily更新。在他们选择日期之前,我不希望出现错误消息,我只希望下拉列表像当前一样为空。我明白了。我会尝试寻找一个jquery解决方案来解决这个问题,因为客户端的错误处理可以避免这个错误。
# GET /arrangements/new
  # GET /arrangements/new.json
  def new
    date = Time.now.to_date.to_s(:db)
    @arrangement = Arrangement.new
    @available_timeslots = Timeslot.where("location_id = ? AND date(timeslot) = ? AND arrangement_id is null", current_user.user_details.location_id, date).order('timeslot ASC')

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @arrangement }
    end
  end

# POST /arrangements
  # POST /arrangements.json
  def create
    @arrangement = Arrangement.new(params[:arrangement])

    respond_to do |format|
      if @arrangement.save
        # update the timslot record with the arrangement id
        if @timeslot = Timeslot.update(@arrangement.timeslot_id, :arrangement_id => @arrangement.id)
            format.html { redirect_to @arrangement, notice: 'Arrangement was successfully created.' }
            format.json { render json: @arrangement, status: :created, location: @arrangement }
        else
            format.html { render action: "new" }
            format.json { render json: @arrangement.errors, status: :unprocessable_entity }
        end
      else
        format.html { render action: "new" }
        format.json { render json: @arrangement.errors, status: :unprocessable_entity }
      end
    end
  end
def create
    @arrangement = Arrangement.new(params[:arrangement])

    respond_to do |format|
      if @arrangement.save
        # update the timslot record with the arrangement id
        if @timeslot = Timeslot.update(@arrangement.timeslot_id, :arrangement_id => @arrangement.id)
            format.html { redirect_to @arrangement, notice: 'Arrangement was successfully created.' }
            format.json { render json: @arrangement, status: :created, location: @arrangement }
        else
            format.html { render action: "new" }
            format.json { render json: @arrangement.errors, status: :unprocessable_entity }
        end
      else

        format.html { render action: "new" }
        format.json { render json: @arrangement.errors, status: :unprocessable_entity }
      end
    end
  end