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-8) #160

Closed
wants to merge 2 commits into from

Conversation

revise-dev[bot]
Copy link

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

The error NoMethodError: undefined method 'symbol' for nil:NilClass occurs when we try to call the symbol method on a nil object. In this case, it's happening because the @stock variable is nil, which means the Stock.find_by(symbol: params[:ticker]) query didn't find a matching stock.

To fix this, we need to use the find_by! method instead of find_by. The bang version (find_by!) will raise an ActiveRecord::RecordNotFound exception if no record is found, rather than returning nil. This is preferable because:

  1. It prevents the NoMethodError from occurring further down in the view.
  2. It automatically triggers Rails' standard 404 handling, which is more appropriate for a record that doesn't exist.
  3. It makes the code's intent clearer - we expect this record to exist, and if it doesn't, it's an exceptional case.

Here's a detailed explanation of the change:

  1. We replace find_by with find_by! in the show action of the StocksController.
  2. This change will now raise an ActiveRecord::RecordNotFound exception if no stock is found with the given ticker symbol.
  3. Rails will automatically catch this exception and render a 404 Not Found response, which is the correct behavior when a requested resource doesn't exist.
  4. This prevents the error from bubbling up to the view, where it was previously trying to call symbol on a nil object.

This change maintains the original intent of the code (finding a stock by its ticker symbol) while adding a layer of error handling that improves the user experience and application robustness.

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
Copy link
Member

/revise write tests for this as well

Copy link
Author

revise-dev bot commented Oct 3, 2024

To address the comment "write tests for this as well", we need to add tests for the StocksController, specifically for the show action. We'll create a new test file spec/controllers/stocks_controller_spec.rb (assuming RSpec is being used for testing).

The tests will cover two scenarios:

  1. When a valid stock ticker is provided, it should return a successful response and assign the correct stock.
  2. When an invalid stock ticker is provided, it should raise an ActiveRecord::RecordNotFound exception.

These tests will ensure that our controller behaves correctly both for existing and non-existing stocks, validating the change we made to use find_by!.

Updated files:

  • spec/controllers/stocks_controller_spec.rb
  • app/controllers/stocks_controller.rb

@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