Amber Framework
WebsiteBlogGithubDiscord
  • Introduction
  • Getting Started
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params
      • Cookies
      • Filters
      • Flash
      • Redirection
      • CSRF
    • Views
      • Basic View Helpers
      • Form Builder
    • Models
      • Granite
        • Granite's README
        • Migrations
        • Validations
        • Callbacks
        • Associations
        • Querying
        • Bulk Insertions
      • Jennifer
        • Jennifer Docs
        • Migrations
        • Models
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
    • Testing
      • System Tests
  • Deployment
    • Manual Deploy
    • Digital Ocean
    • Heroku
    • Dokku
  • CLI
    • New
    • Recipes
    • Plugins
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
  • Examples
    • Amber Auth
    • Crystal Debug
    • Minimal Configuration
  • Cookbook
    • From Scratch
    • Hello World
    • CORS
    • File Download
    • File Upload
    • Cookies
    • Authenticate
    • JSON API
    • JSON Mapping
    • WebSocket Chat
  • Troubleshooting
  • In Production
  • Contributing
  • Code of Conduct
  • HAVE A QUESTION?
    • Join the Discord
    • Follow on Twitter
    • Submit an issue
Powered by GitBook
On this page
  1. Guides
  2. Controllers

Halt!

Sometimes we don't want a request to continue executing; maybe an error has occurred or perhaps an operation is taking too long and we would like to abort the execution of the request. Amber provides a halt! method to controllers.

When you want a request to cease and return a particular message rather then rendering a page, you use Amber halt! to stop the request from continue execution.

class UserController < ApplicationController
  def index
    halt!(403, "Forbidden") if params[:user_id].nil?
    render "index.slang"
  end
end

A status code of 403 was returned, and the content in render will not be delivered to the client.

The next time you’re building an Amber application, consider using halt to simplify error handling.

Halt! and redirect_to

Unlike other frameworks Amber redirect_to stops the current execution of the request and performs the redirection at that given time.

For example, in other frameworks you will have to do something similar to:

class UserController < ApplicationController
  def index
    if some_condition
        if some_condition
            redirect_to(path_one) and return
            # Or another approach
            return redirect_to(path_one)
        end
    end
  end
end

As you probably noticed there is an explicit return, this explicit return is something that is not needed with Amber. Since redirect_to uses the halt! method in the background.

PreviousRequest & Response ObjectsNextRespond With

Last updated 2 years ago