Rails time_select未正确分配给模型时间列

时间:2012-08-31 09:09:21

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

我刚从rails 2.3.14升级到3.2.2。 我的time_select在升级之前工作正常。 现在,在我分配参数后,模型'start_time'和'end_time'列总是为零。

这是视图:

        <%=label_tag("Start Time")%>
    <div class="datetime-select">
        <%=time_select(:calendar_event,:start_time,{:minute_step => 5},{:class=>'time'})%>
    </div>
    <%=label_tag("End Time")%>
    <div class="datetime-select">
        <%=time_select(:calendar_event,:end_time,{:minute_step => 5},{:class=>'time'})%>
    </div>

以下是控制器中的参数:

  Parameters: {"authenticity_token"=>"aGF3mZbZvIDiv6ikH0M8Up2HoTqyXiAFqk+6Wsjbq6g=", "calendar_event"=>{"start_date"=>"Fri Aug 31 10:30:00 +1200 2012", "calendar_resource_id"=>"", "start_time(1i)"=>"2012", "organisation_id"=>"", "start_time(2i)"=>"8", "start_time(3i)"=>"31", "booking_type"=>"WORKER", "start_time(4i)"=>"10", "end_time(1i)"=>"2012", "id"=>"", "start_time(5i)"=>"30", "end_time(2i)"=>"8", "user_id"=>"31", "end_time(3i)"=>"31", "end_time(4i)"=>"11", "description"=>"ssss", "end_time(5i)"=>"00", "end_date"=>"Fri Aug 31 11:00:00 +1200 2012"}}

这是将属性分配给模型的代码:

      @calendar_event=CalendarEvent.new(params[:calendar_event])
      logger.info(@calendar_event.inspect)

以下是日志语句中CalendarEvent模型的内容:

#<CalendarEvent id: nil, description: "ssss", booking_type: "WORKER", start: nil, end: nil, start_date: "2012-08-31", end_date: "2012-08-31", start_time: nil, end_time: nil, allDay: false, calendar_resource_id: nil, organisation_id: nil, user_id: 31, group_session_id: nil, created_at: nil, updated_at: nil>

start_time和end_time总是为零。

它们在schema.rb中定义为“时间”列。

1 个答案:

答案 0 :(得分:0)

这可能是批量分配问题,并且您没有启用警告。尝试将以下内容添加到您的模型中:

attr_accessible :start_time, :end_time

您还可以将此添加到您的development.rb环境中,以获得有关这些类型错误的通知:

# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict