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

时间:2013-04-04 17:03:36

标签: ruby-on-rails ruby ruby-on-rails-3

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

如果提交的表单没有在我的下拉列表中选择的值,我正在尝试显示一条很好的错误消息。目前我收到此错误:undefined method map'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为空

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

答案 2 :(得分:0)

诀窍是在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