Getting errors and params back to view

I asked this question here: Redirecting back on errors to multi-form pages - Rails - Ruby-Forum
but let me try to put it more succinctly.

Below I show a controller action controller meant to save a new item
with its association. The page with the orignating form has displays of
other instances of the same model type and parent and associations
thereof.

If a user enters information for a fposition with a field entry that
fails validation, I can’t get the error messages and information back to
the view.

If I just render the view, the view seems to be missing all the instance
variables it needs. (instance variables only last one action I guess).
If I redirect to the view action that initially rendered the view, that
action creates a new object without the errors and partially completed
data from the first attempt.

def create_fposition
@fposition = Fposition.new(params[:fposition])
@formation = session[:formation]
@coach = session[:coach]
#need help here
if @fposition #! Help here please…need a condition !!!
@formation.fpositions << @fposition
@coach.fpositions << @fposition
@coach.save #btw, do i need this save or does the << get saved
w/o it?
session[:formation] = @formation
session[:coach] = @coach
@fposition = Fposition.new
redirect_to :action => ‘formations_fpositions’
else
#! Help here please ! If I use the render below I get errors
#based on nil object in select box - how else can I do it?
render :action => ‘formations_fpositions’
end
end

People must deal with this over and over but I can’t find it in the
books or forums. Thanks in advance.

On Nov 19, 9:52 pm, Tom N. [email protected]
wrote:

the view.
@coach = session[:coach]
#need help here
if @fposition #! Help here please…need a condition !!!

@fposition will not be nil because it an Fposition object.(a new
Fposition object can be nil?) maybe you want to do @fposition.valid?

Jean-sébastien Jney wrote:

On Nov 19, 9:52 pm, Tom N. [email protected]
wrote:

the view.
@coach = session[:coach]
#need help here
if @fposition #! Help here please…need a condition !!!

@fposition will not be nil because it an Fposition object.(a new
Fposition object can be nil?) maybe you want to do @fposition.valid?

Yes, thanks, I got that part. I should have put that in.

My problem is more how do I get the errors from either .valid? or the
failed save back to the page that has the form on it.

If I render the page, I have errors due to the lack of instance
variables.

If I redirect_to :action that sets the initial page, that page sets new
instanc variables. I guess I could save the object with errors in
session, then have the calling action always check that session slot for
information before it renders the page?

Is using session the way to pass imcomplet parameters and associated
error messages to another action?

Tom,

Can you use a before_filter to setup the common instance variables?
This way you can use render to display validation errors without
issues of the select boxes.

For example:

before_filter :setup :only =>
[:formations_fpositions, :create_fposition]

def setup
@formation = session[:formation]
@formation_fpositions = @formation.fpositions
end

HTH,
Nicholas

On Nov 19, 4:44 pm, Tom N. [email protected]

The before filter could work that way, although I’d probably need to
make it apply only to specific situations where sufficient information
was present. It would clean up some duplicated code at the same time.

I didn’t know that the before filter triggered before renders… I
thought it was only before calling an action method.

Thank you for the idea.