Skip to content

Commit

Permalink
Document redirect with custom status code
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeek committed Jun 29, 2014
1 parent cfe555e commit 4fd6755
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Expand Up @@ -498,6 +498,22 @@ action = Create.new
action.call({ article: { title: 'Hello' }}) # => [302, {'Location' => '/articles/23'}, '']
```

You can also redirect with a custom status code:

```ruby
class Create
include Lotus::Action

def call(params)
# ...
redirect_to 'http://example.com/articles/23', status: 301
end
end

action = Create.new
action.call({ article: { title: 'Hello' }}) # => [301, {'Location' => '/articles/23'}, '']
```

### Mime types

Lotus::Action automatically sets the `Content-Type` header, according to the request.
Expand Down
20 changes: 19 additions & 1 deletion lib/lotus/action/redirect.rb
Expand Up @@ -19,7 +19,7 @@ module Redirect
#
# @since 0.1.0
#
# @example
# @example With default status code (302)
# require 'lotus/controller'
#
# class Create
Expand All @@ -30,6 +30,24 @@ module Redirect
# redirect_to 'http://example.com/articles/23'
# end
# end
#
# action = Create.new
# action.call({}) # => [302, {'Location' => '/articles/23'}, '']
#
# @example With custom status code
# require 'lotus/controller'
#
# class Create
# include Lotus::Action
#
# def call(params)
# # ...
# redirect_to 'http://example.com/articles/23', status: 301
# end
# end
#
# action = Create.new
# action.call({}) # => [301, {'Location' => '/articles/23'}, '']
def redirect_to(url, status: 302)
headers.merge!(LOCATION => url)
self.status = status
Expand Down

0 comments on commit 4fd6755

Please sign in to comment.