I asked this question here: http://www.ruby-forum.com/topic/132105#new 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 19, 9:52 pm, Tom Norian <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I asked this question here:http://www.ruby-forum.com/topic/132105#new > 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 !!!@fposition will not be nil because it an Fposition object.(a new Fposition object can be nil?) maybe you want to do @fposition.valid?> @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. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jean-sébastien Jney wrote:> On Nov 19, 9:52 pm, Tom Norian <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > 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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Norian <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Jean-sébastien Jney wrote: > > On Nov 19, 9:52 pm, Tom Norian <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---