Actions

In a Hanami application, actions are responsible for handling HTTP requests. Actions decide what HTTP response your application returns for a given request - its status, body, headers, whether to issue a redirect, and so on.

Every action in your Hanami application is an individual class. Actions define a #handle method which takes two arguments: request, an object representing the incoming request, and response, an object representing the outgoing response.

Modifying the response object allows you to control how your application responds to a request.

  1. # app/actions/home/show.rb
  2. module Bookshelf
  3. module Actions
  4. module Home
  5. class Show < Bookshelf::Action
  6. def handle(request, response)
  7. name = request.params[:name]
  8. response.body = "Welcome to Bookshelf #{name}!"
  9. end
  10. end
  11. end
  12. end
  13. end

As the code above suggests, the request object provides access to the parameters associated with the incoming request through a #params method.

Let’s start by taking a look at action parameters.