Skip to content

Uninitialized constant Phlex::SGML::Attributes::Date` with Ruby 3.4 (standalone, no Rails) #974

@rehali

Description

@rehali

Environment

  • Ruby: 3.4.x
  • Phlex: 2.4.1
  • Context: Standalone script, no Rails

Steps to reproduce

Create a Gemfile:

source "https://rubygems.org"
gem "phlex"

Run bundle install, then create test.rb:

require "phlex"

class MyComponent < Phlex::HTML
  def view_template
    input(type: "text", disabled: false)
  end
end

puts MyComponent.new.call

Run with ruby test.rb.

Error

test.rb:7:in 'Phlex::SGML::Attributes#generate_attributes': 
uninitialized constant Phlex::SGML::Attributes::Date (NameError)
    when Date
         ^^^^
Did you mean? Data

Cause

lib/phlex/sgml/attributes.rb references Date and Time in a case/when
value serialiser (lines 31–34) with no require "date" anywhere in the file:

when Date
  v.iso8601
when Time
  v.respond_to?(:iso8601) ? v.iso8601 : v.strftime("%Y-%m-%dT%H:%M:%S%:z")

Date lives in Ruby's standard library, not core, and must be explicitly
required. It is not auto-loaded in Ruby 3.4 standalone contexts. Rails/ActiveSupport
loads Date very early as a side effect, which is why this error does not
surface in a Rails app — it only appears when using Phlex without Rails.

Time does not cause the same error because Ruby's core library loads it
automatically.

Suggested fix

Add require "date" at the top of lib/phlex/sgml/attributes.rb:

# frozen_string_literal: true

require "date"

module Phlex::SGML::Attributes
  # ...
end

Workaround

Add require "date" to your own files alongside require "phlex":

require "phlex"
require "date"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions