Fix: NoMethodError (MAYBE-MARKETING-3W) #271
Open
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.
The error
NoMethodError: undefined method '[]' for nil
occurs in several select dropdown components when attempting to display the currently selected value. The issue arises whenoptions.find
returnsnil
because no option matches the currentvalue
, and then the code attempts to access an array index ([0]
or[1]
) on thisnil
result.The original code assumed that there would always be a matching option in the array, but this isn't guaranteed. For example, if the
value
prop contains a value that doesn't match any option's second element,find
returnsnil
.The fix provides a fallback using the Ruby OR operator (
||
) to default to the first option in the array when no match is found. This maintains the original intent of displaying a selected value while preventing the nil error. For example, if a value of "invalid_period" is passed but the options only contain ["months", "months"] and ["years", "years"], instead of erroring, it will display "months".The changes ensure graceful degradation while maintaining the component's functionality, making it more robust against invalid or unexpected input values.
Error Details
Summary:
Stacktrace:
Tip
You can make revisions or ask questions of Revise.dev by using
/revise
in any comment or review!/revise Add a comment above the method to explain why we're making this change.
/revise Why did you choose to make this change specifically?
Important
If something doesn’t look right, click to retry this interaction.
Quick links: View in Sentry • View in Revise