0

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

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

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

看法

<%= 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 %>

控制器

# 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

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

4

3 回答 3

0

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

于 2013-04-04T17:46:36.920 回答
0

我会尝试这样的事情。

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

  ...
end

在视图中

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

诀窍是添加@available_timeslots = []else 子句

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
于 2013-04-05T16:59:32.387 回答