mysql - 在 Rails 应用程序中实时显示打开/关闭

标签 mysql ruby-on-rails ruby

您好,我正在尝试在 Rails 应用程序中创建开放或封闭的显示,我有两个模型“Establishments”和“Opentimes”。这是我的数据库表。我使用 MySQL 作为数据库。

create_table "establishments", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
  t.string   "name"
  t.string   "tagline"
  t.text     "description",    limit: 65535
  t.string   "postcode"
  t.string   "address_line_1"
  t.string   "address_line_2"
  t.string   "address_line_3"
  t.string   "parish"
  t.string   "phone_number"
  t.integer  "user_id"
  t.datetime "created_at",                   null: false
  t.datetime "updated_at",                   null: false
  t.index ["user_id"], name: "index_establishments_on_user_id", using: :btree
end

create_table "opentimes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
  t.integer  "day"
  t.string   "label"
  t.time     "open"
  t.time     "closed"
  t.datetime "created_at",       null: false
  t.datetime "updated_at",       null: false
  t.integer  "establishment_id"
end

我在两个模型之间设置了关系并嵌套了路由。

Rails.application.routes.draw do

 devise_for :users
 resources :establishments do
  resources :opentimes
 end
 root to: "establishments#index"
end

这是我尝试在我的机构模型中执行但无济于事的方法。

class Establishment < ApplicationRecord
  belongs_to :user
  has_many :opentimes

 def open?
    day_of_week_number = Time.current.strftime("%u")
    opentime = @opentimes.find_by(day: day_of_week_number)
    if (opentime.open.strftime("%H:%M") >= Time.current.strftime("%H:%M")) && (Time.curent.strftime("%H:%M") < opentime.closed.strftime("%H:%M"))
        'Open'
    else
        'Closed'
    end
 end
end

如果有人能帮助我并为我指出正确的方向,我将非常感激。如果我对此感到困惑,请原谅我,因为我是 Rails 和编程的新手。非常感谢。

最佳答案

您的条件错误,并且有拼写错误。编码完全按照我用英语所说的:

如果 current_time 则表示已打开已过(大于)开放时间: Time.current.strftime("%H:%M") >= opentime.open.strftime("%H:%M")

如果 current_time 则表示已打开在关闭时间之前(小于): Time.current.strftime("%H:%M") < opentime.closed.strftime("%H:%M")

将这两者结合起来,让我知道结果如何。你的第一个不等式是错误的,你拼写了 currentcurent在你的第二个中。

关于mysql - 在 Rails 应用程序中实时显示打开/关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40250235/

相关文章:

ruby-on-rails - 为什么 Rails titlecase 会在名称中添加空格?

ruby - Rspec:电子邮件正文中的 click_link

ruby - 将数组结果转换为 ruby

MySQL 使用 AVG 和 STD 条件选择排除异常值的结果

mysql - Shell - 一行查询

ruby-on-rails - 有没有更好的方法来检查子参数?

ruby-on-rails - rails 路线。这个过程是如何明确地工作的?

php - Joomla 安装期间无法连接到 mySQL 数据库

php - 使用 php 和 mysql 选择、更新记录

ruby-on-rails - 来自 Sidekiq worker : How to pass a partial? 的邪恶 PDF 生成