【问题标题】:How to handle time duration input in rails app如何处理rails应用程序中的持续时间输入
【发布时间】:2016-11-28 04:58:47
【问题描述】:

我试图找出处理时间字段的最佳方法。我有烹饪时间和准备时间来准备我正在开发的食谱应用程序,如果我可以允许用户基本上在文本字段中写入他们选择的内容,然后将其转换为存储时间,然后使用 rails @ 987654321@ 函数来显示它。还没有找到任何东西可以为我指明正确的方向

编辑:所以我尝试使用下面的 Minutizer,但我不断得到 ​​p>

Chronic:Module 的未定义方法“minutize”

我使用下面的代码创建了一个新模块chronic.rb,并将其保存在我的app/libs 文件夹中

然后我在我的模型中包含了 Chronic,但我无法摆脱上面的错误

【问题讨论】:

标签: ruby-on-rails


【解决方案1】:

在 Chronic gem 中尝试这个 Pull Request - https://github.com/mojombo/chronic/pull/231

它能够处理以下持续时间:

  • 一分半钟
  • 半小时
  • 1.5 小时
  • 甚至更多

在此处粘贴代码(https://github.com/TalkativeTree 的 PR),因为它可能会被删除:

  class Minutizer

    def minutize(text)
      text = pre_normalize(text)
      extract_time(text)
    end

    def pre_normalize(text)
      text.gsub!(/half an/, 'a half')
      text.gsub!(/half a/, 'a half')
      text = Numerizer.numerize(text.downcase)
      text.gsub!(/an hour/, '1 hour')
      text.gsub!(/a hour/, '1 hour')
      text.gsub!(/a day/, '1 minute')
      text.gsub!(/a minute/, '1 day')
      text.gsub!(/a week/, '1 week')
      # used to catch halves not covered by Numerizer. Use for the conversion of 'an hour and a half' Previous gsubs will have changed it to '1 hour and haAlf' by this point.
      text.gsub!(/(\d+)\s?\w+?\s?and haAlf/) do |m|
        m.gsub!(/\A\d+/, ($1.to_f + 0.5).to_s )
      end
      text.gsub!(/\s?and haAlf/, '')
      text.gsub!(/haAlf/, "0.5")
      text.gsub!(/(seconds|secnds|second|secnd|secs)/, 'sec')
      text.gsub!(/(minutes|minute|mintues|mintes|mintue|minte)/, 'min')
      text.gsub!(/(horus|hours|huors|huor|hrs|hr)/, 'hour')
      text.gsub!(/(days|day|dy)/, 'day')
      text.gsub!(/(weeks|wks|wk)/, 'week')
      text
    end

    def extract_time(text)
      minutes = extract(text, 'week')
      minutes += extract(text, 'day')
      minutes += extract(text, 'hour')
      minutes += extract(text, 'min')
      minutes += extract(text, 'sec')
    end

    def extract(text, option)
      total = text.match(/(\d+.\d+|\d+)\s?(#{option})/)

      return 0 unless total
      digitize_time(total[1], option)
    end

    def digitize_time(time, option)
      case option
      when 'week'
        multiplier = 7 * 24 * 60
      when 'day'
        multiplier = 24 * 60
      when 'hour'
        multiplier = 60
      when 'min'
        multiplier = 1
      when 'sec'
        multiplier = 1.to_f/60
      end

      return multiplier * time.to_f
    end
  end

只需将上述文件保存在某处,然后在下面尝试:

您还需要安装 'numerizer' gem 才能使 minutizer 工作。

☁  Documents  irb
2.3.0 :001 > require './minutizer.rb'
 => true
2.3.0 :002 > require 'numerizer'
 => true
2.3.0 :003 >  Minutizer.new.minutize('1.5 hours')
 => 90.0
2.3.0 :004 >

【讨论】:

  • 我很难弄清楚如何让它工作......我从你的 github 安装了慢性 gem,但它似乎也没有 minutizer。我怎样才能覆盖它并将其添加进去。你能给我快速的步骤,告诉我在模型文件中调用它需要做什么。谢谢
  • @DRing 更新了答案。你不需要慢性宝石,但你需要数字宝石
  • 好的,所以在我的rails控制台中为我的应用程序工作。现在我将尝试将其集成到我的表单中,以便在保存之前更改输入
  • 感谢您的帮助,我终于让它工作了...现在我只需要修改 distance_of_time_in_words 以以我喜欢的方式输出...感谢所有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 2010-11-27
相关资源
最近更新 更多