Allow component inheritance with child template overriding parent inline render #2485
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Even though the best practice docs suggest not using inheritance, I hope you will consider this fix since it is a regression from 3.x
The steps to reproduce and fix was created with Claude after we found this issue in our project. I have manually checked the code and verified that it solves our issue.
Steps to reproduce
This error occurs when:
component.sliminstead ofcomponent.html.slim)Create parent component:
app/components/parent_component.rb:app/components/parent_component.slim(note: no.htmlin filename):Create child component that inherits from parent:
app/components/child_component.rb:app/components/child_component.slim(note: no.htmlin filename):Render the component:
Expected behavior
The component should render successfully, using the child component's template and displaying "Hello, World!".
Actual behavior
The application raises a
NoMethodErrorduring component compilation:Root cause: When template files are named without an explicit format (e.g.,
component.sliminstead ofcomponent.html.slim), theActionView::Resolver::PathParserreturnsnilfor the format. The template validation code at line 120 ofcompiler.rbattempts to call.upcaseon thisnilvalue.Workaround: Rename template files to include explicit format:
Note: This worked without issues in ViewComponent 3.x. This appears to be a regression in the template validation logic introduced in ViewComponent 4.x.
Backtrace:
The error occurs at this line in
compiler.rb:System configuration
Rails version: 8.0
Ruby version: 3.4
Gem version: 4.1.0