有人可以提供以下CSV解析问题的建议:
CSV:
Robert,Lobos,lobs@email.com
Klein,Kleinerer,kln@gmail.com
Gross,Grosserer,gr@grosserer.de
方法:
def upload
if (params[:contactList])
csv_content = params[:contactList].read
@recipients = {}
CSV.parse(csv_content) do |row|
@recipients[row[0]] = {'forename' => row[0], 'surname' => row[1], 'email' => row[2]}
end
render 'index'
end
end
目标是在模板中呈现如下值:
<% @recipients.each do |recipient| %>
<option value="test"><%= recipient['forename'] %> <%= recipient['surname'] %> (<%= recipient['email'] %>)</option>
<% end %>
目前引发:
无法将String转换为整数
实现上述目标的最佳方式/ quickfix是什么?
答案 0 :(得分:1)
要使用块迭代哈希,哈希条目的key
和value
将提供给块。
<% @recipients.each do |recipient| %>
应该是
<% @recipients.each do |key, recipient| %>