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