Skip to content

Commit

Permalink
Merge pull request #236 from stephannv/rendering_block
Browse files Browse the repository at this point in the history
Fix `ArgumentError: 'nil' is not an ActiveModel-compatible` when rendering blocks
  • Loading branch information
joeldrapper authored Nov 26, 2024
2 parents cc3703e + 70119dd commit 5b7f064
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ def render(*args, **kwargs, &block)
when nil
partial = kwargs.delete(:partial)

if partial # this is a hack to get around https://github.com/rails/rails/issues/51015
return raw(
@_context.view_context.render(partial, **kwargs) do |*yielded_args|
capture(*yielded_args, &block)
end,
)
end
if partial # this is a hack to get around https://github.com/rails/rails/issues/51015
return raw(
@_context.view_context.render(partial, **kwargs) do |*yielded_args|
capture(*yielded_args, &block)
end,
)
else
return super
end
end

output = if block
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/rendering_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

class RenderingController < ApplicationController
def standard_phlex
render Rendering::StandardPhlex.new
end

def partial_from_phlex
render Rendering::PartialFromPhlex.new
end
Expand Down
17 changes: 17 additions & 0 deletions test/dummy/app/views/rendering/standard_phlex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Rendering
class StandardPhlex < ApplicationView
def view_template
render Header do
h1(id: "title") { "Hello Phlex!" }
end
end

class Header < ApplicationComponent
def view_template(&)
render(&)
end
end
end
end
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get "/helpers/missing_helper", to: "helpers#missing_helper"
get "/helpers/notice", to: "helpers#notice_test"

get "/rendering/standard_phlex", to: "rendering#standard_phlex"
get "/rendering/partial_from_phlex", to: "rendering#partial_from_phlex"
get "/rendering/view_component_from_phlex", to: "rendering#view_component_from_phlex"
get "/rendering/phlex_component_from_erb", to: "rendering#phlex_component_from_erb"
Expand Down
6 changes: 6 additions & 0 deletions test/phlex/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require "test_helper"

class RenderTest < ActionDispatch::IntegrationTest
test "rendering standard phlex" do
get "/rendering/standard_phlex"
assert_response :success
assert_select "h1#title", "Hello Phlex!"
end

test "rendering partial from Phlex view" do
get "/rendering/partial_from_phlex"
assert_response :success
Expand Down

0 comments on commit 5b7f064

Please sign in to comment.