Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Document nested models #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,37 @@ This will mark the `:log_entry` parameters hash and any subhash of it permitted.
You can also use permit on nested parameters, like:

``` ruby
params.permit(:name, {:emails => []}, :friends => [ :name, { :family => [ :name ], :hobbies => [] }])
params.permit(
:name,
{:emails => []},
:friends => [ :name, { :family => [ :name ], :hobbies => [] }]
)
```

This declaration whitelists the `name`, `emails` and `friends` attributes. It is expected that `emails` will be an array of permitted scalar values and that `friends` will be an array of resources with specific attributes : they should have a `name` attribute (any permitted scalar values allowed), a `hobbies` attribute as an array of permitted scalar values, and a `family` attribute which is restricted to having a `name` (any permitted scalar values allowed, too).

Thanks to Nick Kallen for the permit idea!

## Nested models

Permitting nested model attributes works like nested parameters, but you permit the key names, not the nested model names.

``` ruby
class Company
validates :name, presence: true
accepts_nested_attributes_for :people
end
```

In the controller:

``` ruby
params.require(:company).permit(
:name,
{:people_attributes => []}
)
```

## Require Multiple Parameters

If you want to make sure that multiple keys are present in a params hash, you can call the method twice:
Expand Down