////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


== chain



=== Purpose


Uses flash.html[flash] storage to implicitly retain the model following an HTTP redirect from one action to another.


=== Examples


[source,groovy]
----
def shawshankRedemption = new Book(title: 'The Shawshank Redemption')
chain(action: "details", model: [book: shawshankRedemption])
----


=== Description


Usage:

[source,groovy]
----
chain(controller*, action, id*, model, params*)
----

Arguments:

* `uri` - The full uri to redirect to (example /book/list, book/show/2)
* `controller` (optional) - The controller to redirect to; defaults to the current controller if not specified
* `namespace` (optional) - the namespace of the controller to chain to
* `action` - The action to redirect to, either a string name or a reference to an action within the current controller
* `id` (optional) - The id to use in redirection
* `model` - The model to chain to the next action
* `params` (optional) - Parameters to pass to the action chained to.

The chain method stores the passed model in flash scope and then performs an HTTP redirect. The model is then restored for the next request. The model that will eventually be passed to the view will be created by starting with the model that was passed to the chain method and then adding to that all of the data that is in the model returned by the next action, which may replace some of the data that was in the model originally passed to the chain method. For example:

[source,groovy]
----
def one() {
    chain action: 'two', model: [name: 'Tony', town: 'Birmingham']
}

def two() {
    [name: 'Anthony', country: 'England']
}
----

If a request is sent to the `one` action, that action chains to the `two` action which returns a model.  The model that is passed to the view would contain `[name: 'Anthony', town: 'Birmingham', country: 'England']`

The chain method requires either a URI to redirect to *or* a controller/action/id name combination
