未填充MongoDB/Mongoid和Rails 3的DateTime [英] DateTime with MongoDB/Mongoid and Rails 3 Not Populating

查看:53
本文介绍了未填充MongoDB/Mongoid和Rails 3的DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我模型中的代码

 include Mongoid::Document
 include Mongoid::Timestamps

 field :message, :type => String
 field :send_at, :type => DateTime

这是我的部分表格的代码

Here is the code for my form partial

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

但是永远不会填充日期和时间. 我确保Mongo和Mongoid也是最新的. 不知道是否有我想念的东西.

But the date and time is never populated. I made sure that Mongo and Mongoid are up to date as well. Not sure if there's something I'm missing.

[更新日志条目]

Started POST "/notifis" for 127.0.0.1 at Mon Oct 18 05:48:05 -0400 2010
Processing by NotifisController#create as HTML
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"}}
MONGODB noti_development['notifis'].insert([{"send_at(2i)"=>"10", "created_at"=>Mon Oct 
18 09:48:05 UTC 2010, "is_sent"=>false, "updated_at"=>Mon Oct 18 09:48:05 UTC 2010, 
"_id"=>BSON::ObjectID('4cbc17d5c24d7602bc00002d'), "send_at(3i)"=>"18", 
"message"=>"Sample Message", "send_at(4i)"=>"09", "send_at(1i)"=>"2010", 
"send_at(5i)"=>"48"}])
Redirected to http://localhost:3000/notifis
Completed 302 Found in 4ms


Started GET "/notifis" for 127.0.0.1 at Mon Oct 18 05:48:05 -0400 2010
Processing by NotifisController#index as HTML
MONGODB 
noti_development['users'].find({:_id=>BSON::ObjectID('4cb9db18c24d7602bc000007')}, 
{}).limit(-1)
MONGODB noti_development['notifis'].find({}, {})
Rendered notifis/index.html.erb within layouts/application (42.0ms)
Completed 200 OK in 52ms (Views: 51.2ms)

推荐答案

Mongoid尚未处理日期等多参数属性,因此您需要执行以下操作:

Mongoid does not handle multiparameter attributes like Date yet, so you need to do the following:

# copied from: https://gist.github.com/315227
# add this to a new file in your lib directory
module MultiParameterAttributes
  def filter_time(attributes, name)
     attrs = attributes.collect do |key, value|
       if key =~ /^#{Regexp.escape(name.to_s)}\((\d+)(\w)\)$/
         [$1.to_i, value.send("to_#$2")]
       end
     end.compact.sort_by(&:first).map(&:last)
     Time.zone.local(*attrs) unless attrs.empty?
  end
end

# include the module above in your application_controller.rb
class ApplicationController < ActionController::Base
  include MultiParameterAttributes
end

# and in the controller action where you process the form params, use filter_time
class YourController < ApplicationController
  def your_action
    time = filter_time(params, :my_time_attribute_name)
  end
end

此处有更多信息: http://groups.google.com/group/mongoid/browse_thread/thread/f83cbdd641581912

这篇关于未填充MongoDB/Mongoid和Rails 3的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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