0

I want to create multiple objects by submitting a form with one single textarea input.

I envision:
- each line, a new object
- attributes are separated by commas

E.g. for a User model with the attributes: name, email, activated

Larry Page, [email protected], false
Jeff Bezos, [email protected], true
Mark Zuckerburg, [email protected], false

How would I, in the controller, convert the form submission to one array for each "object" (such that I then can iterate over them and perform creation)?

2
  • You can't do that in a pretty form, you have to construct your array in the controller Commented Dec 31, 2014 at 13:12
  • ahhh okei, sorry for missunderstood Commented Dec 31, 2014 at 13:19

1 Answer 1

2

In your controller you could turn your user input into an array supposing that each line is separated by a newline character:

textfield_value = "Larry Page, [email protected], false\nJeff Bezos, [email protected], true\nMark Zuckerburg, [email protected], false"

textfield_value.each_line do |d|
  row = d.split(",")
  MyModel.create(name: row[0], email: row[1], activated: row[2]
end

This can be refactored further. Just giving you an idea.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.