-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These are a combination of existing 1.3 materials and new work, including by @swilgosz Relevant source PRs: #99 #101 hanami/guides-v2#1
- Loading branch information
1 parent
eec4e2b
commit 4d99edf
Showing
11 changed files
with
1,385 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
version: v2.0 | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: Actions | ||
order: 50 | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
title: Overview | ||
order: 10 | ||
aliases: | ||
- "/actions/overview" | ||
--- | ||
|
||
## 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. | ||
|
||
```ruby | ||
# app/actions/books/show.rb | ||
|
||
# frozen_string_literal: true | ||
|
||
module Bookshelf | ||
module Actions | ||
module Books | ||
class Show < Bookshelf::Action | ||
def handle(request, response) | ||
id = request.params[:id] | ||
|
||
response.body = "You requested book #{id}" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
``` | ||
|
||
Let's take a deeper look. | ||
|
||
## Parameters | ||
|
||
The parameters associated with an incoming request are available via the `#params` method on the `request` object. | ||
|
||
Available parameters are surfaced from variables specified in the router (`/books/:id`), query strings (`/books?page=2`), and request bodies (for example a `POST` request to `/books` from a form submission). | ||
|
||
```ruby | ||
def handle(request, response) | ||
# GET /books/1 | ||
request.params[:id] # => "1" | ||
|
||
# GET /books?category=history&page=2 | ||
request.params[:category] # => "history" | ||
request.params[:page] # => "2" | ||
end | ||
``` | ||
|
||
**Remainder of parameters under construction** | ||
|
||
|
||
## Under construction | ||
|
||
Topics to cover: | ||
|
||
- request and response | ||
- mime types and format | ||
- control flow | ||
- params (including req.ar.to_h) and query string parameters. | ||
- param validation | ||
- request headers | ||
- response headers | ||
- redirects | ||
- halt | ||
- exception handling | ||
- HTTP caching | ||
- inheritence | ||
- Testing | ||
|
||
Relevant PRs: | ||
|
||
[https://github.com/hanami/guides/pull/101](https://github.com/hanami/guides/pull/101) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
title: Architecture | ||
order: 60 | ||
--- |
Oops, something went wrong.