I have a fairly complex view that has multiple forms, lots of validations on those forms, paginations, and other features. When validations fail, I like to use render becau
First, these are not compiler error - its run-time errors.
Second, you should either check your data in the controller to make sure its being served properly for rendering OR do some conditional blocks in the view in order to cope with this different data structures.
Lastly, redirect_to
is just a technique of moving the user around, it could be used here but you still need to handle those errors, even in the redirected-to page...
HTH
You should grasp things in their perspective.
Why is render
used instead of redirect
:
when you use render
, you pass the instantiated object
this object, newly created or updated, received some params
when attempting to save the object, validation was triggered and, if unsuccessful, added errors to the current instance
so your object in memory contains validation errors.
But when you use redirect, you restart with a fresh stack which doesn't know anything about the former object in memory, there could not be any magic:
either the object is saved and you can get the persisted data from database
or if it's not saved, you can have some information you previously stored in session
To answer your question a bit closer: before you use render, you have to instantiate all objects needed by the page.
It's just logic the view fails if the expected instance variables are missing.