# Validation failture

**URL:** <https://www.ruby-forum.com/t/validation-failture/171421>\
**Category:** Rails\
**Created:** [July 2, 2009, 6:27am UTC](https://www.ruby-forum.com/t/validation-failture/171421 "2009-07-02T06:27:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [July 2, 2009, 6:27am UTC](https://www.ruby-forum.com/t/validation-failture/171421/1 "2009-07-02T06:27:02Z")

</div>

Say controller x, action a has a form on it that submits to controller  
y,  
action b, and validation fails during the submit. I want to display the  
result of controller x, action a with the validation messages.

In psuedocode, I am here:

’  
controller y

action b  
if !item.save  
# Do something here  
end  
end

I’m having trouble with the do somethign here section. I tried “render  
:template ‘x/A’” but that didn’t load all the required information.  
What is  
the best practice for cross controller validation failures?

Thanks  
Jim Englert.

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [July 2, 2009, 6:36am UTC](https://www.ruby-forum.com/t/validation-failture/171421/2 "2009-07-02T06:36:24Z")

</div>

[http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html](http://guides.rubyonrails.org/activerecord_validations_callbacks.html)

Start with this to get yourself familiar with how validation\_callbacks  
work. Generally, if you are using a form in a view (form\_for as an  
example) you have a form that is backed by the model. Therefore, you  
are placing the validation checks in the model, not the controller.

## Viewer – has form\_for… Controller

Model  
– validates\_presence\_of :name  
– validates\_…\_ … etc

Depending on the type of validation you provide in the model, rails will  
automatically catch the error if it occurs.

Keep in mind that if you are using form\_tag you cannot perform model  
validation callbacks because that form is not backed by a model. You’d  
have to use javascript, or something else to do validation checks.

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [July 2, 2009, 7:13am UTC](https://www.ruby-forum.com/t/validation-failture/171421/3 "2009-07-02T07:13:10Z")

</div>

Each case you will have one approach that better resolve your needs.

Firstly, you should avoid this kind of situation, off course this is not  
possible always but if you do RESTFul apps this kind of situation is not  
so  
usual.

Well, going direct to your problem, you should load all dependencies on  
second controller, and yes, this lead you to an not DRY environment.

First, you should not have many variables being assigned per action,  
this is  
a bad practice, anyway, you can have a module to encapsulate filters  
included in both controllers… this can sound not practical, but in  
truth  
this avoid you to forget to alter the behavior in one side without  
affect  
the other…

I want that this tip was helpful 😉

On Thu, Jul 2, 2009 at 1:26 AM, James E.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ```
> if !item.save
> 
> ```
> 
> >

–  
Everton J. Carpes  
Mobile: +55 53 9129.4593  
MSN: removed\_email\_address@domain.invalid  
UIN: 343716195  
Jabber: removed\_email\_address@domain.invalid  
Twitter: [http://twitter.com/everton\_carpes](http://twitter.com/everton_carpes)  
Blog: [http://www.geek.com.br/blogs/832697633-My-Way](http://www.geek.com.br/blogs/832697633-My-Way)  
Feeds: [https://www.google.com/reader/shared/05200358440305782625](https://www.google.com/reader/shared/05200358440305782625)

“If art interprets our dreams, the computer executes them in the guise  
of  
programs!” - Alan J. Perlis

---

<div class="post-metadata">

**Author:** ![chero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@chero](https://www.ruby-forum.com/u/chero)\
**Post date:** [July 3, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/validation-failture/171421/4 "2009-07-03T16:44:04Z")

</div>

Thats pretty much what I have. Problem is, I still need code in the  
controller I am submitting to that looks like this:

if !item.save

# Handle save validation failure

end

Are you saying that I should not need code like that in my submit  
controller? If so, is there anything special that needs to occur for  
this  
to work?

Thanks,

On Thu, Jul 2, 2009 at 12:36 AM, Ã„lphÃ¤ BlÃ¼Ã« \<
