Fix error: NoMethodError (MAYBE-MARKETING-6) #159
Closed
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 'slug' for nil:NilClass
occurs because@tool
isnil
when trying to access itsslug
method. This suggests thatTool.presenter_from
is not finding a tool based on the provided parameters.The root cause is likely that the
slug
parameter is not being passed correctly to theshow
action. In Rails, when using resourceful routing, the default parameter forshow
actions is:id
, not:slug
. However, thetool_params
method is expecting a:slug
parameter.To fix this, we're adding a fallback mechanism in the
show
action:This line ensures that if
:slug
is not present in the params hash, it will use the value of:id
instead. This way, whether the route is defined with:slug
or:id
, we'll always have a value to search for.The
||=
operator is used here, which assigns the right-hand value only if the left-hand side is nil or false. This means if:slug
is already set, it won't be overwritten.After this change,
tool_params
will always have a value for:slug
, allowingTool.presenter_from
to find the correct tool, thus preventing theNoMethodError
.This solution maintains compatibility with both slug-based and id-based URLs, making the controller more flexible and robust.
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?