多选菜单和虚拟属性 [英] Multi Select menus and Virtual Attributes

查看:41
本文介绍了多选菜单和虚拟属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含 start_at:datetime end_at:datetime 的周期表.因为每个时期都在同一日期开始和结束,所以我想将它们分成三个字段而不是两个字段.

字段 1:日期字段 2:开始的时间00:00"字段3:结束时间00:00"

我开始并完成了这项工作.但不幸的是,我遇到了一个问题.

所以请看一下周期模型,表格和错误,纠正我,纠正代码.

提前致谢!

时期模型

class Period 

期间架构

#只是周期表的模式create_table "期间", :force =>真的做 |t|t.整数show_id"t.datetime "start_at"t.datetime "end_at"t.datetime "created_at"t.datetime "updated_at"结尾

_form.html.erb

 

<%= f.label :show_id, "Show", :class =>:标签%><%= f.collection_select :show_id, @shows.collect, :id, :name %><span class="description">选择节目</span>

<div class="group"><%= f.label :show_date, "Date at", :class =>:标签%><%= f.date_select :show_date, :class =>'datetime_select' %><span class="description">演出日期</span>

<div class="group"><%= f.label :start_at_time, "Start at", :class =>:标签%><%= f.time_select :start_at_time, {:minute_step =>15、:ignore_date =>真}, :class =>'datetime_select' %><span class="description">时间::什么时候开始?</span>

<div class="group"><%= f.label :end_at_time, "End at", :class =>:标签%><%= f.time_select :end_at_time, {:minute_step =>15、:ignore_date =>真}, :class =>'datetime_select' %><span class="description">时间::什么时候结束?</span>

<div><button class="button" type="submit"><%= image_tag("tick.png", :alt => "Save") %>保存

错误

==框架跟踪分配多参数属性时出现 3 个错误==框架跟踪/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3073:in `execute_callstack_for_multiparameter_attributes'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3028:in `assign_multiparameter_attributes'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2750:in `attributes='/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2628:in `update_attributes'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in `send'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in `perform_action_without_filters'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:617:in `call_filters'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'/home/amrnt/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in `ms'/usr/lib/ruby/1.8/benchmark.rb:308:在实时"/home/amrnt/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in `ms'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:in`perform_action_without_flash'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:in`perform_action'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in `send'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in `process_without_filters'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:in `sass_old_process'/usr/lib/ruby/gems/1.8/gems/haml-2.3.0/lib/sass/plugin/rails.rb:19:in `process_without_compass'/home/amrnt/cc/vendor/gems/compass-0.8.17/lib/compass/app_integration/rails/action_controller.rb:7:in `process'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:in `process'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:87:in `dispatch'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in `_call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:in `build_middleware_stack'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in `call'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in `call'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:in `cache'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:in `call'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:in `call'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in `run'/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in `call'/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `each'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `call'/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:17:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/chunked.rb:15:in `call'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:64:in `process'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in`process_client'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in`初始化'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in`初始化'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:34:in `run'/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'脚本/服务器:3==请求参数:{期间"=>{开始时间(4i)"=>20",开始时间(5i)"=>00","end_at_time(4i)"=>"23",end_at_time(5i)"=>30","show_date(1i)"="2010","show_date(2i)"=>"3","show_date(3i)"="31","show_id"="1"},"_method"=>"put","authenticity_token"=>"pm/cXQQnOCsidcAj/OEJuMtHgfppZvoqq09epdETAxo=",id"=>46"}

解决方案

ActionView 中包含的日期和时间表单助手会生成特殊的表单元素,这些元素使用称为多参数分配"的东西.这意味着当您提交 date_selectdatetime_selecttime_select 表单字段时,它们会生成几个单独的参数,这些参数由 ActiveRecord 编译为一个日期、日期时间或时间对象.在您的情况下,这些参数如下所示:

 "start_at_time(4i)"="20",开始时间(5i)"=>00""end_at_time(4i)"=>"23",end_at_time(5i)"=>30""show_date(1i)"="2010","show_date(2i)"=>"3",show_date(3i)"=>31"

我应该注意到,这些与您在 Period 模型的 start_at_time=end_at_time= 方法中期望的字符串非常不同,并且您的模型没有一个 show_date= 方法.

带括号的参数格式是 Rails 用于多参数赋值的格式.但是,ActiveRecord 依赖于相应的数据库列来了解提交的多个参数是代表日期、时间还是日期时间对象.这意味着助手不会处理虚拟属性,至少是开箱即用的.无法判断他们期望什么样的对象.

至少有一张有效票:

https://rails.lighthouseapp.com/projects/8994/tickets/2675-support-for-multiparameter-attribute-assignment-on-virtual-attribute-writers

您可以在此链接中找到有关多参数分配和在 Rails 中使用日期表单助手的精彩概述:

http:///www.alexrothenberg.com/2009/05/how-to-use-dates-in-rails-when-your.html

关于此设计的一般改进:如果每个周期在同一日期开始和结束,为什么不使用 date 属性来存储周期日期和两个 time 属性存储开始和结束时间.您可以轻松创建返回从这些属性编译的 start_atend_at 日期时间对象的方法.

您可以或多或少地使用已有的表单助手来设置表单.您还可以轻松地对 end_time 属性设置验证,确保它始终晚于 start_time.

I have periods table that has start_at:datetime end_at:datetime . Because each period begins and end at the same date, I want to separate the in three fields instead of two.

field 1: the date field 2: the time "00:00" of start field 3: the time "00:00" of end

I started and done this. But unfortunately, I faced a problem.

So please take a look at the period model, the form and the error, and correct me, correct the code.

Thanks in advance!

Period model

class Period < ActiveRecord::Base
  belongs_to :show

  def show_date; start_at.nil? ? Time.now.to_date : start_at.to_date; end
  def start_at_time; start_at.nil? ? Time.now.to_time : start_at.to_time; end
  def end_at_time; end_at.nil? ? Time.now.to_time : end_at.to_time; end

  def start_at_time=(start_str)
   self.start_at = DateTime.parse("#{show_date.strftime("%Y-%m-%d")}T#{start_str.strftime("%H:%M:%S")}")
  end

  def end_at_time=(end_str)
   self.end_at = DateTime.parse("#{show_date.strftime("%Y-%m-%d")}T#{end_str.strftime("%H:%M:%S")}")
  end
end

Periods schema

#just the schema for periods table
  create_table "periods", :force => true do |t|
    t.integer "show_id"
    t.datetime "start_at"
    t.datetime "end_at"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

_form.html.erb

  <div class="group">
    <%= f.label :show_id, "Show", :class => :label %>
    <%= f.collection_select :show_id, @shows.collect, :id, :name %>
    <span class="description">Select the show</span>
  </div>

  <div class="group">
    <%= f.label :show_date, "Date at", :class => :label %>
    <%= f.date_select :show_date, :class => 'datetime_select' %>
    <span class="description">Date of the show</span>
  </div>

  <div class="group">
    <%= f.label :start_at_time, "Start at", :class => :label %>
    <%= f.time_select :start_at_time, {:minute_step => 15, :ignore_date => true}, :class => 'datetime_select' %>
    <span class="description">Time :: When does it start?</span>
  </div>

  <div class="group">
    <%= f.label :end_at_time, "End at", :class => :label %>
    <%= f.time_select :end_at_time, {:minute_step => 15, :ignore_date => true}, :class => 'datetime_select' %>
    <span class="description">Time :: When does it end?</span>
  </div>

  <div>
    <button class="button" type="submit"><%= image_tag("tick.png", :alt => "Save") %> Save</button>
  </div>

The Error

==Framework Trace 

3 error(s) on assignment of multiparameter attributes



==Framework Trace 

/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3073:in `execute_callstack_for_multiparameter_attributes'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3028:in `assign_multiparameter_attributes'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2750:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2628:in `update_attributes'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in `send'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in `perform_action_without_filters'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:617:in `call_filters'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/home/amrnt/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
/home/amrnt/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:in `perform_action'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in `send'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in `process_without_filters'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:in `sass_old_process'
/usr/lib/ruby/gems/1.8/gems/haml-2.3.0/lib/sass/plugin/rails.rb:19:in `process_without_compass'
/home/amrnt/cc/vendor/gems/compass-0.8.17/lib/compass/app_integration/rails/action_controller.rb:7:in `process'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:in `process'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:87:in `dispatch'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in `_call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:in `build_middleware_stack'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:in `cache'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:in `call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/head.rb:9:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:114:in `call'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/reloader.rb:34:in `run'
/home/amrnt/.gem/ruby/1.8/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:108:in `call'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/static.rb:31:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `each'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `call'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/rack/log_tailer.rb:17:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/chunked.rb:15:in `call'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:64:in `process'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:159:in `process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `each'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:158:in `process_client'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:285:in `run'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `initialize'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `new'
/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.5/lib/mongrel.rb:268:in `run'
/home/amrnt/.gem/ruby/1.8/gems/rack-1.0.1/lib/rack/handler/mongrel.rb:34:in `run'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/commands/server.rb:111
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3



==Request Parameters:

{"period"=>{"start_at_time(4i)"=>"20",
 "start_at_time(5i)"=>"00",
 "end_at_time(4i)"=>"23",
 "end_at_time(5i)"=>"30",
 "show_date(1i)"=>"2010",
 "show_date(2i)"=>"3",
 "show_date(3i)"=>"31",
 "show_id"=>"1"},
 "_method"=>"put",
 "authenticity_token"=>"pm/cXQQnOCsidcAj/OEJuMtHgfppZvoqq09epdETAxo=",
 "id"=>"46"}

解决方案

The date and time form helpers included with ActionView generate special form elements that use something called "multiparameter assignment". It means when you submit the date_select, datetime_select or time_select form fields, they generate several separate parameters that are compiled by ActiveRecord into one date, datetime or time object. In your case, those parameters look like:

 "start_at_time(4i)"=>"20",
 "start_at_time(5i)"=>"00"

 "end_at_time(4i)"=>"23",
 "end_at_time(5i)"=>"30"

 "show_date(1i)"=>"2010",
 "show_date(2i)"=>"3",
 "show_date(3i)"=>"31"

I should note that these are very different from the strings you're expecting in your Period model's start_at_time= and end_at_time= methods and that your model doesn't have a show_date= method at all.

That parameter format, with parentheses, is what Rails uses for multiparameter assignment. However, ActiveRecord relies on a corresponding database column in order to know whether the multiple parameters submitted represent a date, time or datetime object. That means the helpers won't work on virtual attributes, at least out-of-the-box. There's no way to tell what kind of object they expect.

There's at least one active ticket on this:

https://rails.lighthouseapp.com/projects/8994/tickets/2675-support-for-multiparameter-attribute-assignment-on-virtual-attribute-writers

And you can find a great overview of multiparameter assignment and using date form helpers with Rails at this link:

http://www.alexrothenberg.com/2009/05/how-to-use-dates-in-rails-when-your.html

Regarding general improvement to this design: if every period starts and ends on the same date, why not use a date attribute to store the period date and two time attributes to store the start and end times. You could easily create a methods that return start_at and end_at datetime objects that are compiled from those attributes.

You can set up your form using the form helpers more or less as you already have. You could also easily set up a validation on the end_time attribute, ensuring it's always later in the day than the start_time.

这篇关于多选菜单和虚拟属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆