Skip to content

Commit

Permalink
Improve params guide (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger authored and drujensen committed Jan 30, 2019
1 parent 4a475da commit 220ca77
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion SUMMARY.md
Expand Up @@ -14,7 +14,7 @@
* [Request & Response Objects](guides/controllers/request-and-response-objects.md)
* [Halt!](guides/controllers/halt.md)
* [Respond With](guides/controllers/respond-with.md)
* [Params Validation](guides/controllers/params-validation.md)
* [Params](guides/controllers/params.md)
* [Cookies](guides/controllers/cookies.md)
* [Filters](guides/controllers/filters.md)
* [Flash](guides/controllers/flash.md)
Expand Down
Expand Up @@ -10,6 +10,40 @@ The params object parses the data from the request, this includes:
2. Form input parameters: these are send as HTML form posts
3. Route URL parameters: route url parameters are define within the resource url.

## Basic Usage

Regular String parameters can be accessed using the following methods. Please note

```ruby

This comment has been minimized.

Copy link
@kevinelliott

kevinelliott Jan 30, 2019

Contributor

OOC: Why use ruby here for these code blocks, but crystal in other parts of the documentation?

params[:key] # requires that the param exists already
params[:key]? # does not require that the param exists
```

To set a param use the following syntax

```ruby
params[:key] = "My String"
```

Please note that while the params accepts a Symbol key to be passed, it will be converted to a String automatically. For example the following two statements are equivalent.

```ruby
params[:key] # => "My String"
params["key"] # => "My String"
```

To access the validated params hash. It will only contain entries that have been sucesssfully validated using the Validation API. If you do not use the Validation API this will return an empty Hash.

```ruby
params.to_h
```

To access the entire raw / non-validated params hash:

```ruby
params.to_unsafe_h
```

## Array Parameters

The params object is not limited to one-dimensional keys and values. It can contain nested arrays and hashes. To send an array of values, append an empty pair of square brackets "[]" to the key name
Expand Down Expand Up @@ -177,4 +211,3 @@ class UsersController < ApplicationController
end
end
```

0 comments on commit 220ca77

Please sign in to comment.