ruby – 具有MongoDB / Mongoid和Rails 3的日期时间不填充

前端之家收集整理的这篇文章主要介绍了ruby – 具有MongoDB / Mongoid和Rails 3的日期时间不填充前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的模型中的代码
  1. include Mongoid::Document
  2. include Mongoid::Timestamps
  3.  
  4. field :message,:type => String
  5. field :send_at,:type => DateTime

这是我的表单部分的代码

  1. <%= f.label :send_at %><br />
  2. <%= f.datetime_select :send_at %>

但是日期和时间从未被填充.
我确保Mongo和Mongoid也是最新的.
不知道有没有我错过的东西

[更新日志条目]

  1. Started POST "/notifis" for 127.0.0.1 at Mon Oct 18 05:48:05 -0400 2010
  2. Processing by NotifisController#create as HTML
  3. Parameters: {"commit"=>"Create Notifi","authenticity_token"=>"/hrlnvA2Xn5NqGgCkPFAQV254IHPJEvZoLxOYNNUwhc=","_snowman"=>"☃","notifi"=>{"send_at(2i)"=>"10","is_sent"=>"0","send_at(3i)"=>"18","send_at(4i)"=>"09","message"=>"erwer","send_at(5i)"=>"48","send_at(1i)"=>"2010"}}
  4. MONGODB noti_development['notifis'].insert([{"send_at(2i)"=>"10","created_at"=>Mon Oct
  5. 18 09:48:05 UTC 2010,"is_sent"=>false,"updated_at"=>Mon Oct 18 09:48:05 UTC 2010,"_id"=>BSON::ObjectID('4cbc17d5c24d7602bc00002d'),"message"=>"Sample Message","send_at(1i)"=>"2010","send_at(5i)"=>"48"}])
  6. Redirected to http://localhost:3000/notifis
  7. Completed 302 Found in 4ms
  8.  
  9.  
  10. Started GET "/notifis" for 127.0.0.1 at Mon Oct 18 05:48:05 -0400 2010
  11. Processing by NotifisController#index as HTML
  12. MONGODB
  13. noti_development['users'].find({:_id=>BSON::ObjectID('4cb9db18c24d7602bc000007')},{}).limit(-1)
  14. MONGODB noti_development['notifis'].find({},{})
  15. Rendered notifis/index.html.erb within layouts/application (42.0ms)
  16. Completed 200 OK in 52ms (Views: 51.2ms)

解决方法

Mongoid不处理Date的多参数属性,因此您需要执行以下操作:
  1. # copied from: https://gist.github.com/315227
  2. # add this to a new file in your lib directory
  3. module MultiParameterAttributes
  4. def filter_time(attributes,name)
  5. attrs = attributes.collect do |key,value|
  6. if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
  7. [$1.to_i,value.send("to_#$2")]
  8. end
  9. end.compact.sort_by(&:first).map(&:last)
  10. Time.zone.local(*attrs) unless attrs.empty?
  11. end
  12. end
  13.  
  14. # include the module above in your application_controller.rb
  15. class ApplicationController < ActionController::Base
  16. include MultiParameterAttributes
  17. end
  18.  
  19. # and in the controller action where you process the form params,use filter_time
  20. class YourController < ApplicationController
  21. def your_action
  22. time = filter_time(params,:my_time_attribute_name)
  23. end
  24. end

这里有更多的信息:
http://groups.google.com/group/mongoid/browse_thread/thread/f83cbdd641581912

猜你在找的Ruby相关文章