What's the best approach to import multiple lines from a text_area in a form?
I've tried a quick bodge using FasterCSV but get a NoMethodError:
undefined method `pos' for {"name"=>"Carrots\r\nPeas\r\nRed Onion"}*
def create
FasterCSV.parse(params[:ingredient], {:headers => false, :quote_char => '"', :col_sep => ','}).each do |row_data|
new_record = Ingredient.new('name' => row_data[0])
new_record.save
end
I want to apply the final thing to a model with multiple columns hence the col_sep
If you want to use FasterCSV.parse
on single lines, you need to get simple lines first.
Split the multi-line data first:
params[:ingredient][:name].split.each do |line|
FasterCSV.parse(line, { ... options ... }).each do |row_data|
... etc ...
I might use parse_line
to explicitly communicate I'm working on a single line instead.