Home > Software engineering >  Simple Form Collection expects Array (got String) for Params but I am expecting a String
Simple Form Collection expects Array (got String) for Params but I am expecting a String

Time:11-21

In my Rails application I have a pronouns input field for the method. I want the user to select from an array of strings ['he/him', 'she/her', 'they/them', 'other'].

**schema.rb (users table)** t.string "pronouns"

I have tried so many different things - one array with 4 strings, an array with 4 arrays and only one string because my label and value are the same, and the solution you can see below.

**edit.html.erb**

<%= f.input :pronouns,
      collection: [['he/him', 'he/him'] , ['she/her', 'she/her'], ['they/them', 'they/them'], ['other', 'other']],
      label_method: :first,
      value_method: :second,
      required: true,
      autofocus: true %>

error message when I submit the form

ActionController::BadRequest Invalid request parameters: expected Array (got String) for param pronouns'

But by now, I don't think my form is the issue. I don't refer to the pronouns anywhere besides in my schema file though. I don't know why it is expecting an array. And if the collection option itself expects an array, why my current solution doesn't work.

CodePudding user response:

You can use the select form_helper ex:

<%= f.select :pronouns, ['he/him', 'she/her', 'they/them', 'they/them', 'other'], required: true, autofocus: true %>
  • Related