How to create multiple records from a single textfield in Rails? -
How to create multiple records from a single textfield in Rails? -
i trying have form field called text allows user save each individual paragraph in it's own record. possible rails?
detailsa user hits new action, , fills out field:
<%= f.text_area :text %>
they come in few lines :text text_area:
line 1 of text. line 2 of text. line 3 of text.after submitting form, how separate lines in create action 2 different records (the first containing line 1 , line 2, , sec containing line 3)? know have split based on new lines (split(/\n\n/)), don't know when that, or how code create action facilitate entire process...
thanks, --mark
so (desperately in need of refactoring) answer:
@post = post.new(post_params) @posts_collection = @post.text.to_s.split(/\r\n\r\n/) @posts_collection.each |post| @post = post.new(post_params) @post.save end
basically- split text field array, , save each item in collection new record.
ruby-on-rails-4
Comments
Post a Comment