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

Errors raised when setting config.raise_on_missing_callback_actions = true #926

Open
jagthedrummer opened this issue Oct 18, 2024 · 0 comments

Comments

@jagthedrummer
Copy link
Contributor

Rails 7.2 introduced a new config option to make it so that an error is raised if the only or except list in a before_action mentions a method that doesn't exist on the controller. Currently the test suite in the starter repo gets a lot of failures if that is enabled.

This test will fail if that config option is set to true:

rails test test/controllers/account/scaffolding/completely_concrete/tangible_things_controller_test.rb:28

The individual failures look like this and don't include much information:

Account::Scaffolding::CompletelyConcrete::TangibleThingsControllerTest
  test_should_get_index                                           FAIL (0.70s)
        Expected response to be a <2XX: success>, but was a <404: Not Found>
        test/controllers/account/scaffolding/completely_concrete/tangible_things_controller_test.rb:30:in `block in <class:TangibleThingsControllerTest>'


Failures and errors:
Account::Scaffolding::CompletelyConcrete::TangibleThingsControllerTest
  test_should_get_index                                           FAIL (0.70s)
        Expected response to be a <2XX: success>, but was a <404: Not Found>
        test/controllers/account/scaffolding/completely_concrete/tangible_things_controller_test.rb:30:in `block in <class:TangibleThingsControllerTest>'

If you tail the test log you'll see an error at the bottom of the test run that says this:

AbstractController::ActionNotFound (The reorder action could not be found for the #<Proc:0x0000000158051b40 /Users/jgreen/.asdf/installs/ruby/3.3.4/lib/ruby/gems/3.3.0/gems/cancancan-3.6.1/lib/cancan/controller_resource.rb:15>
callback on Account::Scaffolding::CompletelyConcrete::TangibleThingsController, but it is listed in the controller's
:only option.

Raising for missing callback actions is a new default in Rails 7.1, if you'd
like to turn this off you can delete the option from the environment configurations
or set `config.action_controller.raise_on_missing_callback_actions` to `false`.
):

That message points to a file in the cancancan gem:

https://github.com/CanCanCommunity/cancancan/blob/c63cde14d419474a9da2ddb03956440045348577/lib/cancan/controller_resource.rb#L15

But that line in cancancan is just applying options defined in the controller, so I don't think that cancancan is at fault here.

I think this line is what's causing the problem:

collection_actions = (%i[index new create reorder] + collection_actions) - except

We're always including the reorder action in the list of collection_actions even if the model is not sortable. I think we need to either define an empty reorder method somewhere that it's available to all super scaffolded controllers, or we need to make it so that reorder is only included in the list if the model is actually sortable.

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

No branches or pull requests

1 participant