Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Created August 11, 2013 21:58
Show Gist options
  • Save baroquebobcat/6207047 to your computer and use it in GitHub Desktop.
Save baroquebobcat/6207047 to your computer and use it in GitHub Desktop.
# 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