I have a fairly basic form ''new'' that assigns an @client instance variable based on information in the session''s user data. def new @client = User.find(:first, :conditions => ["id = ?", session[:user_id]]).client end ''new'' passes on a ''case'' parameter to ''create'' in order to create a case object. def create @case = Case.new(params[:case]) if @case.save redirect_to :action => ''show'', :id => @case else render :action => ''new'' end end If @case is valid, it saves and is effectively redirected. If @case doesn''t pass validation, however, @client comes up nil when it re-renders ''new'', giving me an error loading the view. I need to use render rather than redirect_to in order to get flash messages (right?). How might a be able to get around this? Thanks, Peter -- 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 -~----------~----~----~----~------~----~------~--~---
Set the flash message before the redirect_to and it will be on the page that you redirect_to --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Dec-05 00:27 UTC
Re: Trouble maintaining session data through render :action
Ryan wrote:> Set the flash message before the redirect_to and it will be on the page > that > you redirect_toOkay. else flash[:notice] = ''Something went wrong'' redirect_to :action => ''new'' end This doesn''t seem to do anything to <%= error_messages_for ''case'' %> in new.rhtml. Is this what you had in mind? Thanks for your response. Peter -- 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 -~----------~----~----~----~------~----~------~--~---
Phillip Koebbe
2007-Dec-05 00:48 UTC
Re: Trouble maintaining session data through render :action
flash[:notice] is an entirely different beast than error_messages_for. error_messages_for are used by models. They are the things that get created when your validations fail. flash[] on the other hand, is something that you can use in your controllers to provide messages back to your users in the view. Many people actually use :info, :warning, :error in flash[] and have them styled differently in CSS. To see if your flash message is working correctly, do something like <% if flash[:notice] && flash[:notice].strip != '''' %> <div style="color: red"> <%= flash[:notice] %> </div> <% end %> Peace, Phillip On Dec 4, 2007, at 6:27 PM, Peter Marks wrote:> > Ryan wrote: >> Set the flash message before the redirect_to and it will be on the >> page >> that >> you redirect_to > > Okay. > > else > flash[:notice] = ''Something went wrong'' > redirect_to :action => ''new'' > end > > This doesn''t seem to do anything to <%= error_messages_for ''case'' % > > in > new.rhtml. Is this what you had in mind? Thanks for your response. > > Peter > -- > 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 -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Dec-05 00:53 UTC
Re: Trouble maintaining session data through render :action
Phillip Koebbe wrote:> flash[:notice] is an entirely different beast than > error_messages_for. error_messages_for are used by models. They are > the things that get created when your validations fail. flash[] on > the other hand, is something that you can use in your controllers to > provide messages back to your users in the view. Many people > actually use :info, :warning, :error in flash[] and have them styled > differently in CSS. To see if your flash message is working > correctly, do something like > > <% if flash[:notice] && flash[:notice].strip != '''' %> > <div style="color: red"> > <%= flash[:notice] %> > </div> > <% end %> > > Peace, > PhillipAh, that''s right. Thanks for clearing that up. I really haven''t worked with flash messages much. Do you know of way to get error messages using redirect_to? -- 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 -~----------~----~----~----~------~----~------~--~---
It seems that you want to render the new action and show a flash notice and an error_messages_for at the same time, I think this is possible. @record = Record.new if @record.save flash[:notice] = "Success was here ''07" redirect_to record_path else flash[:notice] = "Failure was here ''07" render :action => "new" end I do believe this will allow you to access error_messages_for :record AND the flash[:notice] at the same time. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Dec-05 01:01 UTC
Re: Trouble maintaining session data through render :action
Ryan wrote:> I do believe this will allow you to access error_messages_for :record > AND > the flash[:notice] at the same time.I believe this is possible normally. However, render :action doesn''t seem to work here given the way I have @client defined in terms of session data. -- 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 -~----------~----~----~----~------~----~------~--~---
In your create action, assign @client to a Client object? On Dec 5, 2007 11:31 AM, Peter Marks <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ryan wrote: > > I do believe this will allow you to access error_messages_for :record > > AND > > the flash[:notice] at the same time. > > I believe this is possible normally. However, render :action doesn''t > seem to work here given the way I have @client defined in terms of > session data. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Dec-05 01:18 UTC
Re: Trouble maintaining session data through render :action
Ryan wrote:> In your create action, assign @client to a Client object?It already should be. I tried re-factoring it to this: user = User.find(:first, :conditions => ["id = ?", session[:user_id]]) @client = Client.find(:first, :conditions => ["id = ?", user.client ]) but got the same result. The session data just doesn''t seem to want to go through. -- 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 -~----------~----~----~----~------~----~------~--~---
In your original create action there is no assign of the @client variable, or a user variable. Just a @case. Could you please paste an updated version? On Dec 5, 2007 11:48 AM, Peter Marks <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ryan wrote: > > In your create action, assign @client to a Client object? > > It already should be. I tried re-factoring it to this: > > user = User.find(:first, :conditions => ["id = ?", session[:user_id]]) > @client = Client.find(:first, :conditions => ["id = ?", user.client ]) > > but got the same result. The session data just doesn''t seem to want to > go through. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Ryan Bigg --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Dec-05 02:01 UTC
Re: Trouble maintaining session data through render :action
Ryan wrote:> In your original create action there is no assign of the @client > variable,I didn''t read your last post correctly. I''ve added the @client variable to the create action and everything works great now. I didn''t realize I had to copy that over from the ''new'' action. Thanks the help Ryan! Peter -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Dec-05 07:38 UTC
Re: Trouble maintaining session data through render :action
On 5 Dec 2007, at 02:01, Peter Marks wrote:> > Ryan wrote: >> In your original create action there is no assign of the @client >> variable, > > I didn''t read your last post correctly. I''ve added the @client > variable > to the create action and everything works great now. I didn''t > realize I > had to copy that over from the ''new'' action. Thanks the help Ryan!That was your actual misunderstanding: render :action => ''foo'' doesn''t run the foo action. It just takes the template associated with the foo action and renders it. Fred --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Peter Marks
2007-Dec-05 18:54 UTC
Re: Trouble maintaining session data through render :action
A follow up question on this matter: My form has a number of fields that are selectively shown or hidden when a box is checked, etc. When validation fails and the page re-renders, all of the data is there, but all the form elements that were previously shown or hidden are back to their initial state. Is there an option I can throw into render :action => ''new'' to conditionally show these page elements based on the @case object? Any other ways to get around this issue? Thanks Again, Peter -- 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 -~----------~----~----~----~------~----~------~--~---