Last active
August 29, 2015 14:07
-
-
Save yutackall/5e7959253ebf92e16986 to your computer and use it in GitHub Desktop.
StrongParameters で配列の中身のパラメータチェック ref: http://qiita.com/yutackall/items/51eabf95b2dbdbe71897
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
params.permit(:users => []) |
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
params.permit(:users => []) |
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
{ | |
"users": [ | |
{ "role": "admin", "name": "taro" }, | |
{ "role": "user", "name": "hanako" }, | |
{ "role": "user", "name": "jiro" }, | |
{ "role": "user", "name": "saburo" }, | |
] | |
} |
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
console » users = { | |
» users: [ | |
» { role: 'admin', name: 'taro' }, | |
» { role: 'user', name: 'hanako' }, | |
» { role: 'user', name: 'jiro' }, | |
» { role: 'user', name: 'saburo' }, | |
» ] | |
» } | |
console » parameters = ActionController::Parameters.new(users) | |
console » parameters.require(:users).map { |u| u.permit(:role, :name) } | |
=> [ | |
[0] { | |
"role" => "admin", | |
"name" => "taro" | |
}, | |
[1] { | |
"role" => "user", | |
"name" => "hanako" | |
}, | |
[2] { | |
"role" => "user", | |
"name" => "jiro" | |
}, | |
[3] { | |
"role" => "user", | |
"name" => "saburo" | |
} | |
] |
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
console » parameters.require(:users).map { |u| u.permit(:role) } | |
=> [ | |
[0] { | |
"role" => "admin" | |
}, | |
[1] { | |
"role" => "user" | |
}, | |
[2] { | |
"role" => "user" | |
}, | |
[3] { | |
"role" => "user" | |
} |
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
private | |
def users_params | |
params.require(:users).map { |u| u.permit(:role, :name) } | |
end |
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
private | |
def users_params | |
params.require(:users).map { |u| u.permit(:role, :name) } | |
end |
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
User.create(users_params) |
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
User.create(users_params) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment