Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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 格式化来自参数的日期_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 格式化来自参数的日期

Ruby on rails 格式化来自参数的日期,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,在Grails中,如果我定义了一个区域设置,并在i18n文件上以特定格式放置了一个日期,如(dd/mm/AAAA),如果调用一个请求,如: http://myapp/myaction?object.date=10/12/2013 当我得到print:params.date时,我得到一个日期对象 如何在rails上执行相同的操作?通常rails会为您处理这些操作。例如,表单helperdatetime\u select与一些 确保时间/日期类型在往返过程中仍然有效。标准日期选择器有多种选择 如果

在Grails中,如果我定义了一个区域设置,并在i18n文件上以特定格式放置了一个日期,如(dd/mm/AAAA),如果调用一个请求,如:

http://myapp/myaction?object.date=10/12/2013
当我得到print:params.date时,我得到一个日期对象


如何在rails上执行相同的操作?

通常rails会为您处理这些操作。例如,表单helper
datetime\u select
与一些 确保时间/日期类型在往返过程中仍然有效。标准日期选择器有多种选择

如果这对您不起作用,例如rails没有生成表单,那么(至少)有两个选项

一个选项是monkey patch
HashWithInferenceTaccess
(由请求参数使用)根据键名进行类型转换。它可能看起来像:

module AddTypedKeys

  def [](key)
    key?(key) ? super : find_candidate(key.to_s)
  end

private

  # look for key with a type extension  
  def find_candidate(key)
    keys.each do |k|
      name, type = k.split('.', 2)
      return typify_param(self[k], type) if name == key
    end
    nil
  end

  def typify_param(value, type)
    case type
      when 'date'
        value.to_date rescue nil
      else
        value
    end
  end   
end


HashWithIndifferentAccess.send(:include, AddTypedKeys)
这将按照您描述的方式扩展params[]。要在rais中使用它,可以将其放入初始化器中,例如
confg/initializers/typed_params.rb

要查看它的工作情况,可以使用

params = HashWithIndifferentAccess.new({'a' => 'hello', 'b.date' => '10/1/2013', 'c.date' => 'bob'})
puts params['b.date'] # returns string 
puts params['b'] # returns timestamp
puts params['a'] # returns string
puts params['c'] # nil (invalid date parsed)
然而。。。我不确定这样做是否值得,而且它可能无法与Rails 4/strong参数一起工作


更好的解决方案是在模型中使用虚拟属性。请参阅使用chronic.

执行此操作的一个非常好的示例:
DateTime.parse('10/12/2013')
,这就是您需要的吗?您好,Gerep,如果我这样做,我将需要拦截所有请求。我需要的是:MyModel.new(params[:object])。默认情况下,如果我将日期格式定义为dd/mm/AAAA,rails将正确地将参数转换为对象。