Validation of image type

404 views
Skip to first unread message

Koli Koli

unread,
Feb 20, 2008, 2:05:47 AM2/20/08
to rubyonra...@googlegroups.com
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.
--
Posted via http://www.ruby-forum.com/.

Visit Indonesia 2008

unread,
Feb 20, 2008, 3:07:12 AM2/20/08
to rubyonra...@googlegroups.com
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,


~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
Reinhart Ariando
YM : booking2heaven
Web: http://teapoci.blogspot.com
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*

Visit Indonesia 2008

unread,
Feb 20, 2008, 3:11:13 AM2/20/08
to rubyonra...@googlegroups.com

> 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
------------

<%= error_messages_for "image" %>

Koli Koli

unread,
Feb 20, 2008, 6:52:43 AM2/20/08
to rubyonra...@googlegroups.com
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???

Josh Pencheon

unread,
Feb 20, 2008, 9:48:56 AM2/20/08
to Ruby on Rails: Talk
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

On Feb 20, 11:52 am, Koli Koli <rails-mailing-l...@andreas-s.net>
wrote:

Visit Indonesia 2008

unread,
Feb 20, 2008, 8:03:06 PM2/20/08
to rubyonra...@googlegroups.com
>
> 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.

~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
Reinhart Ariando
YM : booking2heaven
Web: http://teapoci.blogspot.com
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*

Koli Koli

unread,
Feb 21, 2008, 12:25:19 AM2/21/08
to rubyonra...@googlegroups.com

> 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.

Visit Indonesia 2008

unread,
Feb 21, 2008, 1:35:11 AM2/21/08
to rubyonra...@googlegroups.com

> in my view i have
>
> <%= error_messages for 'entry' %>
>


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

tell me if it works.

Koli Koli

unread,
Feb 21, 2008, 1:39:41 AM2/21/08
to rubyonra...@googlegroups.com
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 .

Reply all
Reply to author
Forward
0 new messages