Created
February 9, 2024 20:52
-
-
Save bradgessler/f46a0cede64eca7ca440964dbcbf4639 to your computer and use it in GitHub Desktop.
Super duper forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'phlex' | |
end | |
class Field < Phlex::HTML | |
def template | |
p { "Field" } | |
end | |
end | |
class Form < Phlex::HTML | |
def field | |
render Field.new | |
end | |
def template | |
p { "Form" } | |
yield if block_given? | |
end | |
end | |
class Page < Phlex::HTML | |
def parent | |
if block_given? | |
render Form.new do | |
yield | |
end | |
else | |
Form.new | |
end | |
end | |
def template | |
# This works | |
render Form.new do |parent| | |
parent.field | |
end | |
# This fails with `undefined method `target' for nil` | |
begin | |
parent = Form.new | |
render parent.field | |
rescue => e | |
plain e.inspect | |
end | |
end | |
end | |
Page.new.call $stdout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment