How to permit an array using strong parameters?

question

#1

Hi,
I’m trying to permit an array in strong parameters.

What I’m trying to do is to pass form-data like this:

items[]=a

With Rails, it works if I permit it like this:

params.permit(items: [])

It also works well with Jets when running it with rspec. Yet, it does not when deployed on AWS.

If I do params.inspect, I am receiving:

Locally with test rspec environment:
<ActionController::Parameters {"items"=>["a"]} permitted: false>
Passing it like this:
post '/v1/some/path', params: { "items[]": 'a' }

Remotely with AWS-deployed code:
<ActionController::Parameters {"items[]"=>"a"} permitted: false>

Here’s the “body” parameter of Lambda event:
"body": "{\"items[]\": \"a\"}",

How can it be fixed? Or maybe I am doing it incorrectly?
Thanks!


#2

Wondering if it’s the form helper tags in your form. Tried this and it worked:

app/views/posts/_form.html.erb:

<%= form_with(model: post, local: true) do |form| %>
  <%= form.text_field :title %>
  <%= text_field_tag "post[items][]", "", multiple: true %>
  <%= text_field_tag "post[items][]", "", multiple: true %>
  <%= form.submit %>
<% end %>

app/controllers/posts_controller.rb:

class PostsController < ApplicationController
  def update
    puts "post_params #{post_params.inspect}".color(:yellow)
    # ...
  end

  def post_params
    params.require(:post).permit(:title, [:items => []])
  end
end  

The logs show this:

post_params <ActionController::Parameters {"title"=>"test", "items"=>["a", "b"]} permitted: true>