I'm guessing you're unaware of the Hash#except method ActiveSupport adds to Hash.
It would allow your code to be simplified to:
redirect_to my_path(params.except(:controller, :action, :other_key))
Also, you wouldn't have to monkey patch, since the Rails team did it for you!
While using Hash#except
handles your problem, be aware that it introduces potential security issues. A good rule of thumb for handling any data from visitors is to use a whitelist approach. In this case, using Hash#slice
instead.
params.slice!(:param_to_keep_1, :param_to_keep_2)
redirect_to my_path(params)
I'd be completely happy with the code you originally posted in your question.
[:controller, :action, :other_key].each { |k| params.delete(k) }
Another way to phrase dmathieu's answer might be
params.delete_if { |k,v| [:controller, :action, :other_key].include? k }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With