Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error: NoMethodError (MAYBE-MARKETING-6) #159

Closed
wants to merge 1 commit into from

Conversation

revise-dev[bot]
Copy link

@revise-dev revise-dev bot commented Oct 3, 2024

The error NoMethodError: undefined method 'slug' for nil:NilClass occurs because @tool is nil when trying to access its slug method. This suggests that Tool.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 the show action. In Rails, when using resourceful routing, the default parameter for show actions is :id, not :slug. However, the tool_params method is expecting a :slug parameter.

To fix this, we're adding a fallback mechanism in the show action:

params[:slug] ||= params[:id]

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, allowing Tool.presenter_from to find the correct tool, thus preventing the NoMethodError.

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?

@Shpigford Shpigford closed this Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant