Jump to content

The ultimate community for Ruby on Rails developers.


validate file update before saving the object.

  • Please log in to reply
2 replies to this topic

#1 charanv@gmail.com



  • Members
  • 2 posts

Posted 03 March 2014 - 03:44 AM

I am assigned a task where I need to validate the contents of file before saving the contents. Each line of the file is a model which will be saved in the database. I want some idea on how I can achieve the following:


{ use case }

1. User uploads a file which contains phone numbers. 

2. Validate the contents of the file by looping through each line checking for any integers. 

   a. If the file contains valid data, store it.

   b. If there are some errors display a pop-up window displaying the records with errors will not be inserteyd. At this point the user can click ok which 

        will cause the model to save the valid lines in the file or the user can click cancel which will cancel the entire operation.



The form_for from view uploads the file and the controller can call a method in model to check whether the file is valid. The problem I have is once I pass the errors to the view and the user is presented with the pop-up how do I send a callback to the model whose state needs to saved some how so when I call the model it knows to save the data. 



Any help is greatly appreciated. 





#2 danielhanke



  • Members
  • 12 posts
  • LocationCologne, Germany

Posted 03 March 2014 - 06:37 AM   Best Answer

Hey Charan,


you could simply store all valid numbers but flag them as unconfirmed in case of invalid numbers, and make them identifyable by any sort of set-id.

In case of user-confirmation, mark them as valid. cancelation ( or timeout ) will drop the entire set.


another approach would be to store it in the session first, then to the persistence. depending on your setup, this might be the way to go.

#3 charanv@gmail.com



  • Members
  • 2 posts

Posted 04 March 2014 - 12:02 AM

Hi Signalman,


Thanks for the response. I agree with you, the second approach will be best fit in my case. Appreciate your time. 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users