On Sunday, February 2, 2014 6:22:43 AM UTC, Bizt wrote:
> I've been trying to set default URL parameters in the controller, which will also be used within the view. This is what I've got:
>
> @params = params
>
> defaults = { :date_from => '21/1/21014', :date_to => '21/2/21014', :data=>"Expense" }
> if @params.any?
> @params = defaults.merge(@params)
> else
> @params = defaults
> end
>
> .. 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.
Fred
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ff3b5ddf-c24e-4d3d-a8bb-988f658c2f51%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment