… seems messy, and doesn’t work When params are present it still uses the default params. Not sure where it’s going wrong and search on google yields many varieties. I know using default values in methods is something that I will repeat again so I was wondering what the best practise is for handling this. Thanks
It doesn't work because @params isn't where params are stored. Params is never empty (at a minimum :controller and :action are set) so you can get rid of the else
You could update params in place , ie params.merge!(...).
Personally I wouldn't do this. I'd be more likely to have a controller method called something like get_date_range that would get the dates from params that would do things like parse the strings into actual dates and/or replace missing params with defaults.
...
You could update params in place , ie params.merge!(...).
Would that not use the value from defaults if it was already present in params?
Personally I wouldn't do this. I'd be more likely to have a controller method called something like get_date_range that would get the dates from params that would do things like parse the strings into actual dates and/or replace missing params with defaults.
Personally, I'd say "best practice" is "don't do that"
Instead of passing raw params to a view, build your model object
(or a service object) with them and pass that in. And if you need
defaults for missing attributes specified, the model/s.o. would be
a better place for them.