On 2/3/07, Wes G. [email protected] wrote:
What I mean is that in order to ensure no problems with losing stuff
that you need to render various pages, you have to cover every case
that can cause a re-render of a particular page, and make sure that the
appropriate flash key is set there.
I find using a session attribute much easier, but of course, this brings
up concerns about polluting the session with too much stuff, so that you
need to be vigilant about cleaning up after yourself.
If I understand correctly, couldn’t you just check for a flash
variable at the point of instantiation. For example, in the old
(1.1.6) scaffold code, instead of:
def new
@thing = Thing.new
end
def create
@thing = Thing.new(params[:thing])
if @thing.save
flash[:notice] = ‘Thing was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
You’d have:
def new
@thing = flash[:thing] || Thing.new
end
def create
@thing = Thing.new(params[:thing])
if @thing.save
flash[:notice] = ‘Thing was successfully created.’
redirect_to :action => ‘list’
else
flash[:thing] = @thing
redirect_to :action => ‘new’
end
end
Similar for #edit/#update.
Doesn’t strike me as much more complex; what it does require is that
your records are serializable.
serialized/deserialized between requests, this can cause a particular
action to fail due to inability to deserialize the session.
What’s the limit on session size?
This may lead you to use a “form model” object whose sole purpose is to
hold the state of a form that needs to be persisted across requests so
that you can successfully re-render stuff but now have to try and store
so much into the session. There are other valid architectural reasons
that you would use a “form model” object as well. But the bottom line
is that you may find that you need to use an object just to manage UI
state that then dumps/reads data to/from your model objects.
See the ActiveForm plugin for a nice implementation of a “form model”
object that does ActiveRecord validation.
You could do it that way. You might also consider adding a state
column, and storing the record in the database with one of a few
‘incomplete’ states. Validations would fire only if they’re relevant
for that particular state. This also gives the user the option of
coming back at a later date, or returning after a crash.
Just an idea.