-
-
Notifications
You must be signed in to change notification settings - Fork 733
Fix: Admin visiting non-existent enterprise raises error #13224
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: Admin visiting non-existent enterprise raises error #13224
Conversation
Currently when an admin tries to edit an no-existing enterprise, a NoMethodError is raised. This commit adds a set_enterprise setter method to the enterprises controller that sets the @enterprise instance variable to have the same value as the enterprise object defined in the the edit method; this method also rescues the NotFound error in case the enterprise is not found and redirects the user to the enterprises index page with a error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Tresor11 🙏
Your solution does fix the problem, but looking at the code I can't tell where this @enterprise
is coming from. I wonder if the better fix would be to replace the @enterprise
by @object
, which represent the model we are looking at. It should be loaded properly via Admin::ResourceController
Let's see what other reviewers think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this. It appears that it solves the issue, but it would be good to add a request spec to clarify exactly what it solves.
From what I can see, this change shouldn't be necessary. The EnterprisesController#load_methods_and_fees
method is expected to run after ResourceController#load_resource
. load_resource
calls the ActiveRecord find
method, which should raise an exception if the resource (enterprise) is not found (see https://github.com/dacook/openfoodnetwork/blob/master/app/controllers/admin/resource_controller.rb#L153-L159).
The ResourceController is intended to handle that and redirect you to the index page, just as your fix does (see resource_not_found
).
So it would be good to investigate why that functionality wasn't working in this case. The best way would be to write a request spec as mentioned, then you can poke and prod (eg with binding.pry
) to see what's happening.
Would you be able to have a go at that?
c8ecf63
to
72c855e
Compare
This commit updates the method name to be called required_enterprise since we only expect it to raise an error when the enterprise is not found.
72c855e
to
507705a
Compare
Thanks for the review. @dacook @rioug I agree that reassigning the enterprise instance doesn't feel quite right, since we are already handling the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks for adding the spec! I checked out the spec on master and could see the spec failure, then can see your change fixes it.
I've just added a commit to fix the linter errors and I think it's good 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good ! Thanks @Tresor11 🙏
Hey @Tresor11, welcome 💐 Many thanks for fixing this one. I've reproduced the issue, by logging in as an admin and visiting the enterprise admin path, with a non-existing enterprise name, which triggered the error 500 you described. After staging the PR, repeating the same scenario displayed the error message: The translation is being picked up correctly - repeating the steps before, with FR as a locale: Looks great! Merging 🎉 |
What? Why?
Currently, when an admin tries to edit a no-existing enterprise, a
NoMethodError
is raised.This commit adds a require_enterprise method that raises a
ActiveRecord::RecordNotFound
error when the enterprise is not found and redirects the user to the enterprise index page with an error message.What should we test?
/admin/enterprises/no-extisting/edit
you should be redirected to the enterprises path with an error messageRelease notes
Changelog Category (reviewers may add a label for the release notes):