# Acts\_as\_attachment and validations / errors

**URL:** <https://www.ruby-forum.com/t/acts-as-attachment-and-validations-errors/83726>\
**Category:** Rails\
**Created:** [January 29, 2007, 8:13am UTC](https://www.ruby-forum.com/t/acts-as-attachment-and-validations-errors/83726 "2007-01-29T08:13:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [January 29, 2007, 8:13am UTC](https://www.ruby-forum.com/t/acts-as-attachment-and-validations-errors/83726/1 "2007-01-29T08:13:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [January 29, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-and-validations-errors/83726/2 "2007-01-29T14:03:01Z")

</div>

On 1/29/07, Rick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [January 29, 2007, 9:15am UTC](https://www.ruby-forum.com/t/acts-as-attachment-and-validations-errors/83726/3 "2007-01-29T09:15:01Z")

</div>

> 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 O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [January 29, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-and-validations-errors/83726/4 "2007-01-29T14:50:19Z")

</div>

On 1/29/07, Rick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [January 29, 2007, 2:22pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-and-validations-errors/83726/5 "2007-01-29T14:22:06Z")

</div>

> 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 O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
