If the parameter is an array, how to include it in Stopara's params.permit

If the parameter is an array, how to include it in Stopara's params.permit

Status

I sent the following parameters to one users table to create multiple records at the same time.

 Parameters: {"users"=>[{"name"}=>{"satoshi"}, {"name"}=>{"yuta"}]}

The following stopparas are received

private

def users_params
  params.require(:users).permit(
      :id,
      :name
  )
end

Error that occurred

If you use the permit method after require, I was told "** permit? I don't know such a method **".

.js


[1] pry(#<UsersController>)> params.require(:users).permit
NoMethodError: undefined method `permit'
for #<Array:0x00007f2024a3ee40>from (pry):62:in `users_params'

(The result of binding.pry is output)

Reason

Since params.require (: publisheds) is a ** Array class, the permit method cannot be used. ** **

.js


[2] pry(#<UsersController>)> params.require(:users).class
=> Array
[3] pry(#<UsersController>)> params.require(:users).respond_to?("permit")
=> false

Information: permit passes when the array number is specified

.js


[4] pry(#<UsersController>)> params.require(:users)[0].respond_to?("permit")
=> true

[5] pry(#<UsersController>)> params.require(:users)[0].permit(:name)
=> <ActionController::Parameters {"name"=>"satoshi"} permitted: true>

If you come in an array, you should take them out one by one, right? I tought.

Solution

Turn with map, disassemble the array, permit and return

private

def users_params
  params.require(:users).map do |user|
    user.permit(
      :id,
      :name
    )
  end
end

Why map was available

The specification of = ruby ​​that evaluates a block and has an array as a return value implicitly returns (returns) the last evaluated value.

Why can't each be used?

I turn the array and take an argument, but even if I permit in it, it doesn't return a value? Is the permit method a little different because it is a permit rather than a value change (assignment)?

It's difficult to make small differences. .. .. If you have a ruby ​​master, I would like to ask you a question.

Reference: Link of Rurima

specifications of each https://docs.ruby-lang.org/ja/latest/method/Array/i/each.html

map specifications https://docs.ruby-lang.org/ja/latest/class/Array.html#I_COLLECT

Recommended Posts

If the parameter is an array, how to include it in Stopara's params.permit
How to output the value when there is an array in the array
Get the type of an array element to determine if it is an array
It doesn't work if the Map key is an array
How to retrieve the hash value in an array in Ruby
[Swift] How to get the number of elements in an array (super basic)
How to check if an instance variable is defined in a Ruby class
[Rails] How to display an image in the view
Library to check if it is an IPv4 format string
How to change a string in an array to a number in Ruby
How to add the same Indexes in a nested array
[Ruby] How to batch convert strings in an array to numbers
How to specify an array in the return value / argument of the method in the CORBA IDL file
[Swift5] How to get an array and the complement of arrays
If you want to include the parent class in Lombok's @builder
What to do if Cloud9 is full in the Rails tutorial
Isn't it reflected even if the content is updated in Rails?
How to get the length of an audio file in java
How to write the view when Vue is introduced in Rails?
[Java] How to search for a value in an array (or list) with the contains method
What to do if the prefix c is not bound in JSP
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
How to include Spring Tool in Eclipse 4.6.3?
Memo: [Java] If a file is in the monitored directory, process it.
How to convert an array of Strings to an array of objects with the Stream API
An active hash that can be treated as data even if it is not in the database
How to get the date in java
[Swift] How to set an image in the background without using UIImageView.
[Ruby] How to prevent errors when nil is included in the operation
Is it possible to put the library (aar) in the Android library (aar) and use it?
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
If it is Ruby, it is efficient to make it a method and stock the processing.
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
How to resolve the "OpenSSH keys only supported if ED25519 is available" error
[Swift5] How to compare specific times to determine if it is before or after
How to make a judgment method to search for an arbitrary character in an array
How to ZIP a JAVA CSV file and manage it in a Byte array
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
How to set when "The constructor Empty () is not visible" occurs in junit
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
How to write an if statement to improve readability-java
How to use an array for HashMap keys
How to add sound in the app (swift)
How to solve an Expression Problem in Java
How to build the simplest blockchain in Ruby
How to build an executable jar in Maven
How to check Rails commands in the terminal
What to do if you can't get the text of an element in Selenium
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
Get the value from the array and find out what number it is included in
[Rails] How to delete production environment MySQL data after putting it in the development environment
How to use git with the power of jgit in an environment without git commands
How to request by passing an array to the query with HTTP Client of Ruby
How to check if the characters entered in the Swift Text Field are email addresses
Use a binary search to see if there are any values in the array
How to make an image partially transparent in Processing
How to set the display time to Japan time in Rails
[Java] How to omit the private constructor in Lombok
How far is the correct answer to divide the process?
How to use an array for a TreeMap key
How to write to apply gem Pagy (pagination) to an array