Skip to content

Commit

Permalink
improved respond_to to automatically render an empty 204 response for…
Browse files Browse the repository at this point in the history
… unknown formats
  • Loading branch information
kraih committed Aug 24, 2011
1 parent ef899b6 commit 9b9125b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

1.90 2011-08-24 00:00:00
- Improved respond_to to automatically render an empty 204 response
for unknown formats.
- Improved render_exception and render_not_found to use the current
format if available. (alnewkirk)
- Improved documentation.
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -479,13 +479,12 @@ sub respond_to {

# Fallback
unless ($target) {
return unless $target = $args->{any};
return $self->rendered(204) unless $target = $args->{any};
delete $stash->{format};
}

# Dispatch
ref $target eq 'CODE' ? $target->($self) : $self->render($target);
return 1;
}

sub send_message {
Expand Down Expand Up @@ -956,14 +955,15 @@ Usually refers to a L<Mojo::Message::Response> object.
=head2 C<respond_to>
my $success = $c->respond_to(
$c->respond_to(
json => sub {...},
xml => {text => 'hello!'},
any => sub {...}
);
Automatically select best possible representation for resource from C<Accept>
request header, C<format> stash value or C<format> GET/POST parameter.
request header, C<format> stash value or C<format> GET/POST parameter,
defaults to rendering an empty C<204> response.
Note that this method is EXPERIMENTAL and might change without warning!
$c->respond_to(
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -293,7 +293,7 @@ single C<render> call.
);

And if no viable representation could be found, the C<any> fallback will be
used.
used or an empty C<204> response rendered automatically.

=head2 Helpers

Expand Down
2 changes: 1 addition & 1 deletion t/mojolicious/restful_lite_app.t
Expand Up @@ -23,7 +23,7 @@ get sub {
json => sub { $self->render_json({just => 'works'}) },
html => sub { $self->render_data('<html><body>works') },
xml => sub { $self->render_data('<just>works</just>') }
) or $self->rendered(204);
);
};

# POST
Expand Down

0 comments on commit 9b9125b

Please sign in to comment.