Forum: Ruby on Rails Acts_as_attachment and validations / errors

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C022941e37aa388ad40b8a6abee77c39?d=identicon&s=25 John T. (johnt519)
on 2007-01-29 08:13
(Sorry if this ends up being a duplicate posting. I originally sent it
via gmail to the google group and I don't know when or if it's ever
going to show)

Maybe I'm just looking too closely... I have a simple AAA model:

class BookCover < ActiveRecord::Base
 acts_as_attachment :storage => :file_system, :content_type => :image
 validates_as_attachment
end


with a simple Create controller:


def create

   @book_cover = BookCover.new(params[:book_cover])

    if @book_cover.save
     ' something
   else
     'something else

   end
   redirect_to :action => 'show', :id => @book_cover
 rescue ActiveRecord::RecordInvalid
   render :action => 'new'
 end


I tried to upload an image, that was about 1.5mb in size. It would not
upload and I could not figure out why. Then I saw the docs that said
by default the max size is 1 mb. Fine, setting the :max_size to
2.megabytes let me upload my image.

My question however, is how do I trap that error / validation error?
I'm sure it's something simple that I'm overlooking.


Thanks!
Rick Olson (Guest)
on 2007-01-29 09:15
(Received via mailing list)
> I tried to upload an image, that was about 1.5mb in size. It would not
> upload and I could not figure out why. Then I saw the docs that said
> by default the max size is 1 mb. Fine, setting the :max_size to
> 2.megabytes let me upload my image.
>
> My question however, is how do I trap that error / validation error?
> I'm sure it's something simple that I'm overlooking.

Should be a validation error like any other.  It's just
validates_inclusion_of :size...

--
Rick Olson
http://weblog.techno-weenie.net
http://mephistoblog.com
John Tsombakos (Guest)
on 2007-01-29 14:03
(Received via mailing list)
On 1/29/07, Rick Olson <technoweenie@gmail.com> wrote:
> validates_inclusion_of :size...
>

Thanks.

In the else statement of the save I redirected back to the "new" page
and I put an <%= error_messages_for :book_cover %> But didn't see
anything displayed. I'll have to play around some more..

john
Rick Olson (Guest)
on 2007-01-29 14:22
(Received via mailing list)
> Thanks.
>
> In the else statement of the save I redirected back to the "new" page
> and I put an <%= error_messages_for :book_cover %> But didn't see
> anything displayed. I'll have to play around some more..
>
> john

Redirecting will start a brand new request that usually creates a new
instance of your record, losing any error messages in the process.
You can use render :action => 'new' to show the form again, yet your
@book_cover record maintains its state from the #create action.

--
Rick Olson
http://weblog.techno-weenie.net
http://mephistoblog.com
John Tsombakos (Guest)
on 2007-01-29 14:50
(Received via mailing list)
On 1/29/07, Rick Olson <technoweenie@gmail.com> wrote:
> instance of your record, losing any error messages in the process.
> You can use render :action => 'new' to show the form again, yet your
> @book_cover record maintains its state from the #create action.

Ah, ok. Like I said, I was getting confused. That's what I get from
copying and pasting from various places :)

Thanks!

John
This topic is locked and can not be replied to.