# CSV upload button with validation

**URL:** <https://www.ruby-forum.com/t/csv-upload-button-with-validation/130250>\
**Category:** Rails\
**Created:** [February 26, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/csv-upload-button-with-validation/130250 "2008-02-26T19:38:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 26, 2008, 7:38pm UTC](https://www.ruby-forum.com/t/csv-upload-button-with-validation/130250/1 "2008-02-26T19:38:13Z")

</div>

Hi all,

i urgently need help for my first RoR app which should be running  
already. My app is a shop database ([http://ewallshops.heroku.com/](http://ewallshops.heroku.com/)) made  
with Heroku. Entering shops manually already works fine. Data entered is  
checked for correctness of email adress and uniqueness of the shop name.

Now I need to add a button to enable front-end users to upload .csv shop  
data that, during upload, also should pass the checks. In case of  
errors, the data items containing them should be shown. The others  
(without errors) shall be integrated into my database with continuing  
ids (not those from the csv-file).

Some things about Heroku: As far as I know it uses Rails 2.0.2 and  
postgresql as database.

Thanks in advance!  
Daniel

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 26, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/csv-upload-button-with-validation/130250/2 "2008-02-26T20:19:22Z")

</div>

Sorry to stress, but I really need help with this. I’ve already been  
looking this forum and also tried several solutions, but none of them  
worked. Please help me if you can. thx

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 27, 2008, 12:59am UTC](https://www.ruby-forum.com/t/csv-upload-button-with-validation/130250/3 "2008-02-27T00:59:09Z")

</div>

How far have you gotten?

There are a few things you need to implement:

1. Form which allows the user to upload the file (http://  
[Peak Obsession](http://manuals.rubyonrails.com/read/chapter/57))
2. Controller method that imports the data

This method needs to iterate over each line, splitting the fields on  
‘,’ and create the objects.  
new\_object = Object.new(blah, blah)

You store the failed objects in an array  
failed \<\< new\_object unless new\_object.save

1. If failed contains entries you need to redirect to a form  
presenting the items to be corrected  
if failed.length \> 0 redirect\_to :action =\> ‘fix\_records’

On Feb 26, 2:19 pm, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 27, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/csv-upload-button-with-validation/130250/4 "2008-02-27T15:13:49Z")

</div>

Thanks for the hints, guys. At the moment I decided to go public without  
the csv upload ability, because nothing I tried worked out well and I  
have other projects that need my time. But when I have more time, I’ll  
try to set it up like you told me. I’ll also try to implement the  
loading bar feature described here:

> **[The Construction Blog - Tips on Choosing Local Construction Companies](http://www.blognow.com.au)**
>
> Tips on Choosing Local Construction Companies

which looks very promising (I tried to do that yesterday, but only got  
mongrel errors). I’ll post again in case of success, so stay tuned if  
you’re interested.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 27, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/csv-upload-button-with-validation/130250/5 "2008-02-27T14:52:19Z")

</div>

tip : u might want to use the valid? method of the model.

new\_item = Item.new(:name=\>…, bla bla)

failed\_items = []

if new\_item.valid?  
… do something …  
else  
failed\_items \<\< new\_item  
end
