Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby – 具有MongoDB / Mongoid和Rails 3的日期时间不填充大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的模型中的代码
include Mongoid::Document
 include Mongoid::timestamps

 field :message,:type => String
 field :send_at,:type => datetiR_943_11845@e

这是我的表单部分的代码

<%= f.label :send_at %><br />
 <%= f.datetiR_943_11845@e_SELEct :send_at %>

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

[更新日志条目]

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'),"message"=>"Sample message","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不处理Date的多参数属性,因此您需要执行以下操作:
# 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

大佬总结

以上是大佬教程为你收集整理的ruby – 具有MongoDB / Mongoid和Rails 3的日期时间不填充全部内容,希望文章能够帮你解决ruby – 具有MongoDB / Mongoid和Rails 3的日期时间不填充所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。