Skip to content

Revert params.fetch to params.expect conversions in scaffold#52932

Merged
rafaelfranca merged 1 commit intorails:mainfrom
martinemde:martinemde/revert-params-expect-for-fetch
Sep 16, 2024
Merged

Revert params.fetch to params.expect conversions in scaffold#52932
rafaelfranca merged 1 commit intorails:mainfrom
martinemde:martinemde/revert-params-expect-for-fetch

Conversation

@martinemde
Copy link
Contributor

Motivation / Background

In rails/jbuilder#573 we stumbled on the case where the scaffold was generating params.expect(table_name: {}) which has the unintended consequence of requiring that a param is given when there are no expected params.

Detail

This PR reverts 2 small changes to the scaffold templates introduced by #51674 so that default scaffolds don't return 400 Bad Request upon creation with no attributes.

Additional information

Thanks to @jeromedalbert for noticing that I had made the very changes that I pointed out as problematic in his PR in jbuilder. Teamwork!

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@jeromedalbert
Copy link
Contributor

jeromedalbert commented Sep 14, 2024

I think you need something like this to fix tests: jeromedalbert@e5119cf

@martinemde martinemde force-pushed the martinemde/revert-params-expect-for-fetch branch from 1aca62c to c9f9772 Compare September 15, 2024 15:18
@martinemde
Copy link
Contributor Author

I think you need something like this to fix tests.

Yes, of course. Thank you again, @jeromedalbert. That's what I get for coding in a hurry between other things.

Updated and tested locally. Should be good now.

@martinemde martinemde force-pushed the martinemde/revert-params-expect-for-fetch branch from c9f9772 to 06d034b Compare September 15, 2024 15:49
@martinemde
Copy link
Contributor Author

Updated again to add a test that was missing for api controller generation.

The conversions have the unintended consequence of requiring params
when none are expected. Ensure generated templates work as is.
Adds a missing test for the api controller generation.

Originally pointed out in rails/jbuilder#573.
@martinemde martinemde force-pushed the martinemde/revert-params-expect-for-fetch branch from 06d034b to 1929503 Compare September 15, 2024 16:51
@rafaelfranca rafaelfranca merged commit c435b6c into rails:main Sep 16, 2024
@martinemde martinemde deleted the martinemde/revert-params-expect-for-fetch branch September 17, 2024 20:32
@hachi8833
Copy link
Contributor

hachi8833 commented Nov 6, 2024

Current edgeguides suggest us to use expect, and the descriptions of fetch method for strong parameters has been removed from current edgeguides:

https://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

But the template for controller has been updated to use fetch in the PR. I'm confused with the inconsistency between edgeguide and the generated controller. Which methods should we use primarily -- fetch and expect?

@martinemde do you have any thoughts to resolve the inconsistency?

@martinemde
Copy link
Contributor Author

martinemde commented Nov 6, 2024

@hachi8833 fetch serves a very specific purpose of returning a value that you aren't certain will be there. fetch is like the opposite of expect and should be used in actions where you expect the request to be made without that param at least some of the time.

The reason it was reverted here is that fetch handles the case where there are no params given, returning an empty hash. If expect was used here then the freshly generated scaffold would fail when no params were given.

This is not, in fact, an inconsistency. All generated scaffolds will use expect unless the generator was called without any attributes.

tl;dr: Always use expect unless the params are optional. (or, simply, use expect unless it doesn't do what you want)

@hachi8833
Copy link
Contributor

@martinemde I got it, thank you! 😂

@martinemde
Copy link
Contributor Author

@hachi8833 awesome, thanks for asking. This revert here was actually me catching a mistake in my first PR and fixing it real quick. 😄

@martinemde
Copy link
Contributor Author

martinemde commented Nov 6, 2024

@hachi8833 If you're interested, I wrote an article about how to use params.expect. It's even more wordy and verbose than my replies here 😆 It goes into more detail than would be necessary for the rails guides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants