Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 更新rails中对象数组的值_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 更新rails中对象数组的值

Ruby on rails 更新rails中对象数组的值,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我正在用ruby on rails从数据库检索数据。我必须在controller中用新的格式化日期更新检索到的日期。 像这样的:- @events = 'My Query.' @events.each do |eventdata| # Since the date is in utc i have to convert it in users time zone. eventdata.start_date = eventdata.start_date.strf

我正在用ruby on rails从数据库检索数据。我必须在controller中用新的格式化日期更新检索到的日期。
像这样的:-

   @events = 'My Query.'
   @events.each do |eventdata|
      # Since the date is in utc i have to convert it in users time zone.
      eventdata.start_date = eventdata.start_date.strftime("%Y-%m-%d %H:%M:%S")
    end

但它不起作用。需要一些帮助。

您不能更改字段的格式,因为它是日期时间,您必须使用相应的方法

def formated_start_date
  start_date.strftime("%Y-%m-%d %H:%M:%S")
end
但是,您可以通过创建config/initializers/date_time_formats.rb文件并添加以下内容来更改所有数据库记录的日期时间格式

Time::DATE_FORMATS[:default] = "%Y-%M-%d %H:%M:%S"

您不能更改字段的格式,因为它是DateTime,您必须为此使用方法

def formated_start_date
  start_date.strftime("%Y-%m-%d %H:%M:%S")
end
但是,您可以通过创建config/initializers/date_time_formats.rb文件并添加以下内容来更改所有数据库记录的日期时间格式

Time::DATE_FORMATS[:default] = "%Y-%M-%d %H:%M:%S"

这是一个猴子补丁,用于在转换为json时格式化所有日期时间的日期:

class ActiveSupport::TimeWithZone
  def as_json(options = {})
      strftime('%Y-%m-%d %H:%M:%S')
  end
end
戴上

/config/initializers/json_date_path.rb

仅此而已。

这是一个猴子补丁,用于在转换为json时格式化所有日期时间的日期:

class ActiveSupport::TimeWithZone
  def as_json(options = {})
      strftime('%Y-%m-%d %H:%M:%S')
  end
end
戴上

/config/initializers/json_date_path.rb

仅此而已。

它不起作用。我明白了,它不起作用了。我明白了。我如何更新这个值?eventdata.start\u date=eventdata.Formatted\u start\u date?。。。事实上,我正在将事件对象转换为json,它在日历中使用,因此我必须在控制器中对其进行格式化。我需要更多关于您尝试执行的操作的信息。您试图做的是在对象转换为json之前格式化日期?或者您试图在将日期时间持久化到数据库之前更改日期时间的格式?我试图在对象转换为JSONA之前设置日期格式,如何更新此值?eventdata.start\u date=eventdata.Formatted\u start\u date?。。。事实上,我正在将事件对象转换为json,它在日历中使用,因此我必须在控制器中对其进行格式化。我需要更多关于您尝试执行的操作的信息。您试图做的是在对象转换为json之前格式化日期?或者您试图在将日期时间持久化到数据库之前更改日期时间的格式?我试图在对象转换为JSONTANKS之前设置日期的格式。。。终于解决了…从昨天开始一直在做:)非常感谢。。。终于解决了…从昨天开始一直在做这个:)