Created
August 11, 2013 21:58
-
-
Save baroquebobcat/6207047 to your computer and use it in GitHub Desktop.
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
# example from the documentation | |
# ============================== | |
params = ActionController::Parameters.new({ | |
person: { | |
name: 'Francesco', | |
age: 22, | |
pets: [{ | |
name: 'Purplish', | |
category: 'dogs' | |
}] | |
} | |
}) | |
permitted = params.permit(person: [ :name, { pets: :name } ]) | |
permitted.permitted? # => true | |
permitted[:person][:name] # => "Francesco" | |
permitted[:person][:age] # => nil | |
permitted[:person][:pets][0][:name] # => "Purplish" | |
permitted[:person][:pets][0][:category] # => nil | |
# but look, pets doesn't have to be an array | |
# a hash will also work | |
# ========================================== | |
params = ActionController::Parameters.new({ | |
person: { | |
name: 'Francesco', | |
age: 22, | |
pets: { | |
name: 'Purplish', | |
category: 'dogs' | |
} | |
} | |
}) | |
permitted = params.permit(person: [ :name, { pets: :name } ]) | |
permitted.permitted? # => true | |
permitted[:person][:name] # => "Francesco" | |
permitted[:person][:age] # => nil | |
permitted[:person][:pets][:name] # => "Purplish" | |
permitted[:person][:pets][:category] # => nil | |
# I wonder if person has to be a hash? | |
# Nope | |
# ==================================== | |
ActionController::Parameters.new({ | |
person: [{ | |
name: 'Francesco', | |
age: 22, | |
pets: [{ | |
name: 'Purplish', | |
category: 'dogs' | |
}] | |
}] | |
}) | |
permitted = params.permit(person: [ :name, { pets: :name } ]) | |
permitted.permitted? # => true | |
permitted[:person][0][:name] # => "Francesco" | |
permitted[:person][0][:age] # => nil | |
permitted[:person][0][:pets][0][:name] # => "Purplish" | |
permitted[:person][0][:pets][0][:category] # => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment