-
Notifications
You must be signed in to change notification settings - Fork 468
Description
I'm not sure if this was ever intended to work this way, but in 3.x you could use the component's methods and variables inside of a block passed to a partial. This does not seem to work in 4.0.
Example and reproduction repo below.
I'm sorry I could not grasp how to write up a PR with a failing test :(((
Is there a way to re-enable this behaviour? And if not, how would you handle it?
Steps to reproduce
bin/rails test test/components/test_component_test.rb
Inline example:
# app/components/test_component.rb
class TestComponent < ViewComponent::Base
def initialize(title:)
@title = title
end
attr_reader :title
end
# app/components/test_component.html.erb
<%= render "shared/card" do %>
<%= title %>
<% end %>
# app/views/shared/_card.html.erb
<div>
<%= yield %>
</div>
# app/components/test_component_test.rb
def test_component_renders_something_useful
assert_includes(
render_inline(TestComponent.new(title: "Hello, world!")).to_html,
"Hello, world!"
)
end
Test passes in 3.x (see previous commit in reproduction repo), but fails in 4.0
Expected behavior
title
inside of block passed to card
partial resolves to title
passed to TestComponent
Actual behavior
Partial context does not have access to TestComponent
context.
@title
is nil
title
method is not defined
Backtrace:
ActionView::Template::Error: undefined method `title' for an instance of #<Class:0x0000000125f70280>
app/components/test_component.html.erb:2:in `block in call'
app/views/shared/_card.html.erb:2
app/components/test_component.html.erb:1:in `call'
test/components/test_component_test.rb:8:in `test_component_renders_something_useful'
System configuration
Rails version: 8.0.2.1
Ruby version: 3.3.6
Gem version: 4.0.2 (works in 3.x)