0

I have Rails 5 app. And lets say I have this table (PG ORM):

class CreatePosts < ActiveRecord::Migration[5.0]
  def change
    create_table :posts do |t|
      t.string :title
      t.text :body
      t.text :tags, array: true, default: []

      t.timestamps
    end
  end
end

Now I'd like to see in my form text input with separated by commas tags. And when I hit 'save' button Rails would properly save it in array field. How can I do it?

Nerver Corameiro
  • 130
  • 1
  • 13

1 Answers1

0

To manage the array in the form you can do this:

Eg multiple input fields all with the same name:

<input type="textbox" name="course[track_codes][]" ...>

will become the Array

params["course"]["track_codes"]

with an element for each of the input fields with the same name

Check answer: Ruby on Rails: Submitting an array in a form

Also if you are using simple_form, then you can do this:

form_for @location do |f|
  f.input :zips, as: :array
end
victor sosa
  • 899
  • 13
  • 27