Validation of image type

Hi, I'm using file_column to do the image uploads .i want to know how to do the validation to upload only jpg or png or gif images. RIght now the user can upload any type of file.I want to restrict it only image type. i also want to know how to flash the error message in my view.

Please help.

Hello,

Hi, I'm using file_column to do the image uploads .i want to know how to do the validation to upload only jpg or png or gif images.

class Album < ActiveRecord::Base file_column :image

validates_file_format_of :image, :in => ["gif", "jpg"] validates_filesize_of :image, :in => 1.kilobytes..5000.kilobytes

end

Good Luck,

class Album < ActiveRecord::Base file_column :image

validates_file_format_of :image, :in => ["gif", "jpg"], :message => "Hey, Upload only GIF and JPG format" validates_filesize_of :image, :in => 1.kilobytes..5000.kilobytes

end

Visit Indonesia 2008 wrote:

class Album < ActiveRecord::Base file_column :image

validates_file_format_of :image, :in => ["gif", "jpg"], :message => "Hey, Upload only GIF and JPG format" validates_filesize_of :image, :in => 1.kilobytes..5000.kilobytes

end

------------ in view ------------

Hi, I have a problem the validations are not happening in my model . i even tried validating somethgin else like validates_presence_of :comments where comments is a text field even this validation fails.

Can u please help???

Hi Koli,

I don't know how your app is structured, but have you put the validation rules into your photo model?

I've got a similar app with both album and photo models. VI2008's example puts such validation on an attribute of an album, rather than of a photo.

Depends what your db looks like, really.

Have you got any more details?

Cheers, Josh

Hi, I have a problem the validations are not happening in my model . i even tried validating somethgin else like validates_presence_of :comments where comments is a text field even this validation fails.

Can u please help???

The validation in AR should work properly, as long as the column name in your table is correct, no matter of the type data is for validate_presence_of. if it is not working, you will have error message. If you want validate a field that is not from your table, you should add attr_reader :anyname or attr_accessor :anyname or in your AR you can validate using :

#it is the same like attr_accessor

def anyname=(new_name)   if new_name.nil?      errors.add("anyname", "is empty.")   end end

. yeah such as a josh said, Paste us more details.

The validation in AR should work properly, as long as the column name in your table is correct, no matter of the type data is for validate_presence_of. if it is not working, you will have error message. If you want validate a field that is not from your table, you should add attr_reader :anyname or attr_accessor :anyname or in your AR you can validate using :

Hi, class Entry < ActiveRecord::Base

has_many :problems belongs_to :reply

require 'remote_uploads.rb'

        file_column :image, :magick => {       :versions => { "thumb" =>"50x50"}     }

validates_file_format_of :image, :in => ["gif", "jpg","png"], :message =>"must be a jpg,png or gif file type"

validates_presence_of :comments

end

table structure: comments is a column in the entry table.

in my view i have

<%= error_messages for 'entry' %>

i hope the above info is sufficient.

in my view i have

<%= error_messages for 'entry' %>

the correct is <%= error_messages_for 'entry' %>

tell me if it works.

Visit Indonesia 2008 wrote:

in my view i have

<%= error_messages for 'entry' %>

the correct is <%= error_messages_for 'entry' %>

tell me if it works.

Hi , It was syntax error while typing in the forum.it is as u have mentioned only. THe thing is . i'm doing a image upload to iframe using ajax. is that where the problem lies?? so when i hit the upload button it actually does not show the create action in the firebug .