From 6f5ce21ff5330bec3e1a0099e7de85caf654b8dd Mon Sep 17 00:00:00 2001 From: Aaron Wheeler Date: Wed, 2 Apr 2008 17:25:37 +0900 Subject: [PATCH] Added more robust error message plus additional docs explaining how action-args defaults cannot evaluate to nil. --- merb-action-args/README | 28 +++++++++++++++++++- .../lib/merb-action-args/abstract_controller.rb | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/merb-action-args/README b/merb-action-args/README index b461e30..83be834 100644 --- a/merb-action-args/README +++ b/merb-action-args/README @@ -46,4 +46,30 @@ end The interesting thing here is that hitting "/foo/bar?one=uno&three=three" will call foo("uno", "dos", "three"). In other words, the defaults can be in any order, and -merb-action-args will figure out where to fill in the holes. \ No newline at end of file +merb-action-args will figure out where to fill in the holes. + +There is one important limitation on argument defaults, though: they cannot evaluate +to false. So setting a default as nil or false, and then not passing in that +parameter will raise a BadRequest exception. + +{{[ +class Foo < Merb::Controller + def bar(id = nil) + id || 'there was no id passed in' + end +end +]}} + +Hitting "/bar" will raise a BadRequest. + +Hitting "/bar/1" will call bar(1) + +A good work-around is to use the empty string as the default. + +{{[ +class Foo < Merb::Controller + def bar(id = '') + id.blank? 'there was no id passed in' : id + end +end +]}} diff --git a/merb-action-args/lib/merb-action-args/abstract_controller.rb b/merb-action-args/lib/merb-action-args/abstract_controller.rb index f941907..8bdbda6 100644 --- a/merb-action-args/lib/merb-action-args/abstract_controller.rb +++ b/merb-action-args/lib/merb-action-args/abstract_controller.rb @@ -28,7 +28,7 @@ class Merb::AbstractController args = self.class.action_argument_list[action].map do |arg, default| arg = arg p = params.key?(arg.to_sym) - raise BadRequest unless p || default + raise(BadRequest, "Expected to find value for arg :#{arg}, or a default value that doesn't evaluate to false") unless p || default p ? params[arg.to_sym] : default end __send__(action, *args) -- 1.5.3.1