Model Validations + Urgent

Colin Law wrote:

Hello all,

How to give validation on a field which is not a database column. I mean i am having a file field(upload_resume) in my views. So i want to give validation on it that size should be less then 1mb. I tried self.upload_resume and params[:upload_resume] but both are not working.

Are you trying to check the file size before or after the upload?

Hello Guys, Thanks for your replies.

Yes i want to check the size of file before upload only. I mean first i am checking that file had valid attributes or not. If it has then upload it else dont. Currenly i am doing this in controllers but for cleaner code i want to do it in models with validations.

How are you checking the file attributes in the controller?

Colin

Colin Law wrote:

Hello Guys, Thanks for your replies.

Yes i want to check the size of file before upload only. I mean first i am checking that file had valid attributes or not. If it has then upload it else dont. Currenly i am doing this in controllers but for cleaner code i want to do it in models with validations.

How are you checking the file attributes in the controller?

Just like as if File.size(params[:upload_resume]) > 1mb   redirect_to :back with some flash[:notice] end

The only problem with this is that i am unable to retain the form values after redirecting.

Colin Law wrote:

Hello Guys, Thanks for your replies.

Yes i want to check the size of file before upload only. I mean first i am checking that file had valid attributes or not. If it has then upload it else dont. Currenly i am doing this in controllers but for cleaner code i want to do it in models with validations.

How are you checking the file attributes in the controller?

Just like as if File.size(params[:upload_resume]) > 1mb redirect_to :back with some flash[:notice] end

Is params[:upload_resume] a filename on the users PC? I am probably showing my ignorance here, but how does the server know the size of a file on the users PC? I hope you are not testing this by 'uploading' from a PC which is actually also the server running the rails app, so that the file is directly accessible from the rails app before it is uploaded.

Colin