试图用collection_select保存表单的Rails是空的,为nil提供了未定义的方法`map':NilClass

时间:2022-10-28 21:55:13

I have a form with a drop down generated from a collection_select. The collection_select starts off blank and then when the user selects a date from the date field, the collection_select is updated with values for the date that is chosen.

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

I'm trying to show a nice error message if the form is submitted without a value chosen in my dropdown. Currently i'm getting this error: undefined methodmap' for nil:NilClass`

我正在尝试显示一个很好的错误消息,如果提交的表单没有在我的下拉列表中选择的值。目前我收到此错误:未定义的methodmap'为nil:NilClass`

How can i make it so that if the user doesn't select a value in the dropdown, I can show them a nice error message?

我怎样才能使它如果用户没有在下拉列表中选择一个值,我可以向他们展示一个很好的错误消息?

View

视图

<%= form_for(@arrangement) do |f| %>

    <div class="control-group">
    <%= f.label :date, :class => 'control-label' %>
        <div class="controls">
            <%= f.text_field :date, :class => 'input-large datepicker' %>
        </div>
    </div>

    <div class="control-group">
        <%= f.label :timeslot, :class => 'control-label' %>
        <div class="controls">
            <%= collection_select(:arrangement, :timeslot_id, @available_timeslots, :id, :timeslot_spelled) %>
        </div>
    </div>

<% end %>

Controller

调节器

# 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

The error given is referring to @available_timeslots being empty when trying to save the form

给出的错误是指在尝试保存表单时@available_timeslots为空

3 个解决方案

#1


0  

I would try something like this.

我会尝试这样的事情。

def new
  @available_timeslots = ...
  if @available_timeslots.count > 0
    flash.now[:error] = "nil errrraaarrr"
  end

  ...
end

In view

在视野中

<div class="controls">
  <%- if @available_timeslots.count > 0 %>
    <%= collection_select(:arrangement, :timeslot_id, @available_timeslots, :id, :timeslot_spelled) %>
  <% else %>
    <%= flash.now[:error] %>
  <% end %>
</div>

#2


0  

@available_timeslots is nil. Make sure it's set with an array of available timeslots to avoid this error message.

@available_timeslots是零。确保使用可用时隙数组进行设置以避免出现此错误消息。

#3


0  

The trick was to add @available_timeslots = [] in the else clause

诀窍是在else子句中添加@available_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

#1


0  

I would try something like this.

我会尝试这样的事情。

def new
  @available_timeslots = ...
  if @available_timeslots.count > 0
    flash.now[:error] = "nil errrraaarrr"
  end

  ...
end

In view

在视野中

<div class="controls">
  <%- if @available_timeslots.count > 0 %>
    <%= collection_select(:arrangement, :timeslot_id, @available_timeslots, :id, :timeslot_spelled) %>
  <% else %>
    <%= flash.now[:error] %>
  <% end %>
</div>

#2


0  

@available_timeslots is nil. Make sure it's set with an array of available timeslots to avoid this error message.

@available_timeslots是零。确保使用可用时隙数组进行设置以避免出现此错误消息。

#3


0  

The trick was to add @available_timeslots = [] in the else clause

诀窍是在else子句中添加@available_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