0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#Rails permit nested params ( Action Controller Parameters ) example

Last updated at Posted at 2020-03-22
# Rails like Nested params with ActionController::Parameters instance
params = ActionController::Parameters.new(
  name: 'Alice',
  age: 22,
  contact: ActionController::Parameters.new(
    tel: 07011112222,
    email: 'user@example.com'
  )
)
# <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=><ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: false>} permitted: false>


# Permit only flat params
permitted_params = params.permit(:name, :age)

# params not changed
# see "permitted: false"
params
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=><ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: false>} permitted: false>

# permit return value is permitted only flat params
permitted_params
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22} permitted: true>


# permit all params
params.permit!

# permitted include all nested params
# see flags "permitted: true"
params
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=><ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: true>} permitted: true>

params.permitted?
# => true

params[:contact].permitted?
# => true



# permit only nested params

params2 = ActionController::Parameters.new(
  name: 'Alice',
  age: 22,
  contact: ActionController::Parameters.new(
    tel: 07011112222,
    email: 'user@example.com'
  )
)


contact_permitted_params = params2[:contact].permit(:tel, :email)
# => <ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: true>

partly_permitted_params2 = params2.merge(contact: contact_permitted_params)
# <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=><ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: true>} permitted: false>

partly_permitted_params2.permitted?
# => false

partly_permitted_params2[:contact].permitted?
# => true

# NOTE
# permit! bang does not receive arguments oh no ...

# params2[:contact].permit!(:tel, :email)
# ArgumentError: wrong number of arguments (given 2, expected 0)

# In this way wee need permit nested params with two times execute methods
#
# 1. permit with args 
# 2. permit!

# params2[:contact].permit(:tel, :email)
# => <ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: true>
# params2[:contact].permit!
# => <ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: true>
# params2
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=><ActionController::Parameters {"tel"=>941921426, "email"=>"user@example.com"} permitted: true>} permitted: false>



params_include_nil = ActionController::Parameters.new( name: 'Alice', age: 22, contact: nil )
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=>nil} permitted: false>

# flat permit allowed
params_include_nil.permit(:name, :age, :contact)
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=>nil} permitted: true>

# nexted permit ignored
params_include_nil.permit(:name, :age, contact: [:tel, :email])
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22} permitted: true>



# When nested params has empty values instance
params_include_empty = ActionController::Parameters.new( name: 'Alice', age: 22, contact: ActionController::Parameters.new )
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=><ActionController::Parameters {} permitted: false>} permitted: false>

# NG : When specified as flatten key
#      nested params deleted
params_include_empty.permit(:name, :age, :contact)
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22} permitted: true>

# NG : When specified Empty hash
#      nested params deleted
params_include_empty.permit(:name, :age, contact: [])
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22} permitted: true>

# NG : When specified some key
#      Nothing in permitted nested params but not deleted
params_include_empty.permit(:name, :age, contact: [:tel])
# => <ActionController::Parameters {"name"=>"Alice", "age"=>22, "contact"=><ActionController::Parameters {} permitted: true>} permitted: true>


Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?