【问题标题】:trouble making an "if" statment with time.now on ruby on rails在 ruby​​ on rails 上用 time.now 做出“if”语句时遇到问题
【发布时间】:2017-12-08 00:22:15
【问题描述】:

我正在尝试根据一天中的时间显示一条消息,我目前有这段代码:

<% if Time.now >= "7:00" and Time.now <= "14:00" %>
Good Morning <%= current_user.nome %>! <br>
<% else %>
Good Afternoon <%= current_user.nome %>!
<% end %>

现在是 14:30,页面仍然显示Good Morning (username),此时应该显示Good Evening (username)

  • 我的代码错了吗?
  • 我该如何解决这个问题?

【问题讨论】:

  • 你打印Time.now的返回了吗?
  • 你能比较一下StringTime吗?
  • 2017-07-04 14:54:14 +0100 这是时间。现在
  • 那你如何比较timetime

标签: ruby-on-rails ruby time


【解决方案1】:

尝试:

<% if Time.now >= Time.parse("7:00") and Time.now <= Time.parse("14:00") %>
  Good Morning <%= current_user.nome %>! <br>
<% else %>
  Good Afternoon <%= current_user.nome %>!
<% end %>

注意:Time.now会返回时间对象,而"7:00"是字符串,所以需要将字符串时间转换为时间对象,并进行比较。

【讨论】:

  • 是的,这有效!谢谢,我的代码有什么问题,你能解释一下你为什么使用Time.parse吗?
【解决方案2】:

你可以这样写而不是解析时间你可以简单地写

解决方案:1

<% if Time.now.hour >= 7 && Time.now.hour <= 14 %>
Good Morning <%= current_user.nome %>! <br>
<% else %>
Good Afternoon <%= current_user.nome %>!
<% end %>

解决方案:2

您可以通过使用三元运算符编写更少的代码

(0..11).include?(Time.now.hour) ? "Good Morning <%= current_user.nome %>" : Good Afternoon <%= current_user.nome %>

【讨论】:

  • if Time.now >= "7:00" -- 您正在检查字符串 Time.parse("7:00") --- 这会将字符串转换为时间格式类
  • 14:30 会通过您的hour &lt;= 14 检查,但它不应该。
【解决方案3】:

我认为这是一个辅助方法是值得的:

# in app/helpers/application_helper.rb
def greeting(name)
  greeting = (7..13).cover?(Time.current.hour) ? 'Good Morning' : 'Good Afternoon'

  "#{greeting} #{name}".strip
end

在你看来:

<%= greeting(current_user.nome) %>!

支持不均匀时间的另一个更详细的版本可能如下所示:

def greeting(name)
  greeting = case Time.current.to_s(:time)
             when ('05:00'..'11:59') then 'Good Morning'
             when ('12:00'..'17:59') then 'Good Afternoon'
             when ('18:00'..'21:59') then 'Good Evening'
             else 'Good Night'
             end
  "#{greeting} #{name}".strip
end

【讨论】:

  • 使用 13 而不是 14 会更接近指定的数学。或额外的时间(7...14)
  • 另一种方式:Time.now.hour.between?(7,13)
  • 你会如何处理像 6:30-13:30 这样的时间范围?
  • 很好,虽然我可能会从方法中删除 name 参数并通过 &lt;%= greeting %&gt; &lt;%= current_user.nome %&gt;! 调用它
  • 这是一个选项。但是当有一个名字为空的用户时会发生什么?
【解决方案4】:

我会使用助手来存储方法并保持视图清晰。

class ApplicationHelper
  def greeting
    "Good #{time_greeting} #{current_user.nome}"
  end

  def time_greeting
    return "Morning" if Time.now.hour.between?(7, 13)
    "Afternoon"
  end
end

那么你只能在你的视图中使用问候方法:

<%= greeting %>

【讨论】:

    【解决方案5】:

    我会使用助手:

    def morning?(t = Time.current)
      t.between? t.change(hour: 7), t.change(hour: 14)
    end
    

    t是一个时间实例,默认为当前时间,例如:

    t = Time.current
    #=> Tue, 04 Jul 2017 16:30:09 CEST +02:00
    

    change 设置一个或多个时间元素。如果只指定了:hour,则“更小”的单位(如分钟和秒)设置为 0:

    t.change(hour: 7)  #=> Tue, 04 Jul 2017 07:00:00 CEST +02:00
    t.change(hour: 14) #=> Tue, 04 Jul 2017 14:00:00 CEST +02:00
    

    between? 最后检查t 是否在这些值之间。

    将当前时间作为参数传递可以更容易地测试方法:

    morning? Time.parse('06:59') #=> false
    morning? Time.parse('07:00') #=> true
    morning? Time.parse('14:00') #=> true
    morning? Time.parse('14:01') #=> false
    

    用法:

    <% if morning? %>
      Good Morning <%= current_user.nome %>!
    <% else %>
      Good Afternoon <%= current_user.nome %>!
    <% end %>
    

    【讨论】:

    • 我看不出需要helpers,因为我只需要在索引视图上使用它,无论如何谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多