testdouble/moderate_parameters

RuntimeError in ModerateParameters::Parameters#require

Closed this issue · 3 comments

Hello! First of all thanks for nice tool. It can help to improve old apps. Today I have faced with RuntimeError in ModerateParameters::Parameters#require on latest (0.4.0) version. Any idea how to debug and fix it?

https://github.com/hintmedia/moderate_parameters/blob/2bb97aa11c48e6339ba6f4898cb7d60c70461178/lib/moderate_parameters/parameters.rb#L39

Screenshot 2021-10-21 at 16 04 48

Hey @bestwebua! Thanks for the bug report. For a bit of additional context, what version of Ruby and Rails are you running?

Also, what does self look like in this case, or maybe at least self[key]?

Of course, @kyleboe!

pry(#<ActionController::Parameters>)> self
=> <ActionController::Parameters> # returns ActionController::Parameters instance
pry(#<ActionController::Parameters>)> self[key]
=> "123"
pry(#<ActionController::Parameters>)> Rails::Info
=> About your application's environment
Rails version             5.2.4.4
Ruby version              2.3.6-p384 (x86_64-darwin19)
RubyGems version          2.5.2.2
Rack version              2.2.3
Application root          /Users/user/projects/rails-app
Environment               test
Database adapter          mysql2

Ok I think I understand the issue. require is intended to be used similarly to ActionController::Parameters implementation of require where there is a key with a hash value. Something like this:

params = ActionController::Parameters.new({ user: { name: "test user", email: "test@user.com" } })

params.require(:user)
#=> <ActionController::Parameters {"name"=>"test user", "email"=>"test@user.com"} permitted: false>

The goal of require is not to reach terminal values (such as "123" in this case). You probably just want to use moderate to permit :prescriber_id while you are transitioning to strong params, and then use permit after you have transitioned.

Hopefully that clears things up and if you have any more questions feel free to comment below.