Re: [Rails] upload file, read first 10 lines.
On 26 March 2011 22:06, Colin Law <clanlaw@googlemail.com> wrote:
> On 26 March 2011 18:52, Mauro <mrsanna1@gmail.com> wrote:
>> I have a csv file and in my controller:
>>
>> uploaded_file = params[:upload][:csv].read
>> @rows << FasterCSV.parse(uploaded_file)
>>
>> I want to put in @rows only the first 10 lines but the read method
>> reads all the file.
>
> I think you can do something like
> FasterCSV.foreach( uploaded_file) do |row|
> # do something for first 10 rows then break out
> end
I've solved with:
uploaded_file = params[:upload][:csv].read
FasterCSV.parse(uploaded_file) do |row|
@rows[i] = row
i += 1
break row if i == 10
end
>
> Colin
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home